<template>
  <aside class="product-sidebar">
    <div class="product-sidebar__img-wrap" @click="$emit('edit-avatar')">
      <img
        v-if="avatarUrl"
        :src="avatarUrl"
        :alt="employeeName"
        class="employee-sidebar__avatar"
      />
      <div v-else class="employee-sidebar__avatar employee-sidebar__avatar--fallback">
        {{ initials }}
      </div>
    </div>

    <div class="product-sidebar__meta">
      <h6 class="product-sidebar__name">{{ employeeName }}</h6>
      <p class="product-sidebar__ref">{{ jobTitle || status || "Employe" }}</p>
      <div class="product-sidebar__badges employee-sidebar__badges">
        <span
          class="employee-sidebar__badge"
          :class="isActive ? 'employee-sidebar__badge--success' : 'employee-sidebar__badge--muted'"
        >
          {{ isActive ? "Actif" : "Inactif" }}
        </span>
        <span v-if="isThanatopractitioner" class="employee-sidebar__badge employee-sidebar__badge--info">
          Thanatopracteur
        </span>
      </div>
    </div>

    <div class="employee-sidebar__details">
      <div class="employee-sidebar__detail-item">
        <span class="employee-sidebar__detail-label">Embauche</span>
        <span class="employee-sidebar__detail-value">{{ hireDate }}</span>
      </div>
      <div class="employee-sidebar__detail-item">
        <span class="employee-sidebar__detail-label">Contact</span>
        <span class="employee-sidebar__detail-value">{{ employee.email || employee.phone || "Non renseigne" }}</span>
      </div>
    </div>

    <nav class="product-sidebar__nav">
      <button
        v-for="tab in tabs"
        :key="tab.id"
        class="product-sidebar__nav-item"
        :class="{ 'is-active': activeTab === tab.id }"
        @click="$emit('change-tab', tab.id)"
      >
        <component :is="tab.icon" class="nav-icon" />
        {{ tab.label }}
      </button>
    </nav>
  </aside>
</template>

<script setup>
import { computed, defineComponent, defineProps, defineEmits, h } from "vue";

const props = defineProps({
  avatarUrl: {
    type: String,
    default: null,
  },
  initials: {
    type: String,
    required: true,
  },
  employeeName: {
    type: String,
    required: true,
  },
  jobTitle: {
    type: String,
    default: "Employe",
  },
  status: {
    type: String,
    default: "Actif",
  },
  hireDate: {
    type: String,
    required: true,
  },
  isActive: {
    type: Boolean,
    default: true,
  },
  isThanatopractitioner: {
    type: Boolean,
    default: false,
  },
  activeTab: {
    type: String,
    required: true,
  },
  employee: {
    type: Object,
    required: true,
  },
});

defineEmits(["edit-avatar", "change-tab"]);

const IconOverview = defineComponent({
  render: () =>
    h(
      "svg",
      {
        viewBox: "0 0 16 16",
        fill: "none",
        stroke: "currentColor",
        "stroke-width": "1.5",
      },
      [
        h("path", { d: "M2 8s2.5-4 6-4 6 4 6 4-2.5 4-6 4-6-4-6-4z" }),
        h("circle", { cx: "8", cy: "8", r: "1.75" }),
      ]
    ),
});

const IconInfo = defineComponent({
  render: () =>
    h(
      "svg",
      {
        viewBox: "0 0 16 16",
        fill: "none",
        stroke: "currentColor",
        "stroke-width": "1.5",
      },
      [
        h("circle", { cx: "8", cy: "8", r: "6" }),
        h("path", { d: "M8 7v3M8 5.25h.01" }),
      ]
    ),
});

const IconDocument = defineComponent({
  render: () =>
    h(
      "svg",
      {
        viewBox: "0 0 16 16",
        fill: "none",
        stroke: "currentColor",
        "stroke-width": "1.5",
      },
      [
        h("path", { d: "M5 2.5h4l2.5 2.5v7A1.5 1.5 0 0 1 10 13.5H5A1.5 1.5 0 0 1 3.5 12V4A1.5 1.5 0 0 1 5 2.5z" }),
        h("path", { d: "M9 2.5V5h2.5" }),
      ]
    ),
});

const IconPractitioner = defineComponent({
  render: () =>
    h(
      "svg",
      {
        viewBox: "0 0 16 16",
        fill: "none",
        stroke: "currentColor",
        "stroke-width": "1.5",
      },
      [
        h("circle", { cx: "8", cy: "5", r: "2.5" }),
        h("path", { d: "M3.5 13c.5-2.3 2.3-3.5 4.5-3.5s4 1.2 4.5 3.5" }),
      ]
    ),
});

const IconActivity = defineComponent({
  render: () =>
    h(
      "svg",
      {
        viewBox: "0 0 16 16",
        fill: "none",
        stroke: "currentColor",
        "stroke-width": "1.5",
      },
      [h("path", { d: "M2.5 11.5h2l1.5-3 2.25 4 1.75-3h3.5" })]
    ),
});

const tabs = computed(() => {
  const baseTabs = [
    { id: "overview", label: "Apercu", icon: IconOverview },
    { id: "info", label: "Informations", icon: IconInfo },
    { id: "user", label: "Utilisateur", icon: IconPractitioner },
    { id: "documents", label: "Documents", icon: IconDocument },
    { id: "activity", label: "Activite", icon: IconActivity },
  ];

  if (props.isThanatopractitioner) {
    baseTabs.splice(3, 0, {
      id: "practitioner",
      label: "Praticien",
      icon: IconPractitioner,
    });
  }

  return baseTabs;
});
</script>

<style scoped>
.product-sidebar {
  position: sticky;
  top: 1.25rem;
  display: flex;
  flex-direction: column;
  gap: 0;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  overflow: hidden;
}

.product-sidebar__img-wrap {
  width: 72px;
  height: 72px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid #e5e7eb;
  margin: 1.25rem auto 0;
  flex-shrink: 0;
  cursor: pointer;
  background: #fff;
}

.employee-sidebar__avatar {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.employee-sidebar__avatar--fallback {
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(310deg, #5e72e4 0%, #825ee4 100%);
  color: #fff;
  font-size: 1.25rem;
  font-weight: 700;
}

.product-sidebar__meta {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 0.75rem 1rem 1rem;
  border-bottom: 1px solid #f3f4f6;
}

.product-sidebar__name {
  font-size: 14px;
  font-weight: 700;
  color: #111827;
  text-align: center;
  margin: 0;
}

.product-sidebar__ref {
  font-size: 12px;
  color: #9ca3af;
  margin: 0;
}

.product-sidebar__badges {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 5px;
  margin-top: 4px;
}

.employee-sidebar__badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 24px;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.employee-sidebar__badge--success {
  background: #ecfdf3;
  color: #047857;
}

.employee-sidebar__badge--muted {
  background: #f3f4f6;
  color: #6b7280;
}

.employee-sidebar__badge--info {
  background: #eff6ff;
  color: #1d4ed8;
}

.employee-sidebar__details {
  display: grid;
  gap: 0.5rem;
  padding: 0.9rem 1rem 1rem;
  border-bottom: 1px solid #f3f4f6;
}

.employee-sidebar__detail-item {
  padding: 0.7rem 0.8rem;
  border: 1px solid #eef2f7;
  border-radius: 8px;
  background: #f8fafc;
}

.employee-sidebar__detail-label {
  display: block;
  margin-bottom: 0.2rem;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #8392ab;
}

.employee-sidebar__detail-value {
  display: block;
  color: #344767;
  font-size: 0.82rem;
  word-break: break-word;
}

.product-sidebar__nav {
  display: flex;
  flex-direction: column;
  padding: 0.5rem;
  gap: 2px;
}

.product-sidebar__nav-item {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 9px 12px;
  border-radius: 7px;
  border: none;
  background: transparent;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: #6b7280;
  font-family: inherit;
  text-align: left;
  transition: background 0.12s, color 0.12s;
}

.product-sidebar__nav-item:hover {
  background: #f9fafb;
  color: #111827;
}

.product-sidebar__nav-item.is-active {
  background: #111827;
  color: #fff;
}

.nav-icon {
  width: 15px;
  height: 15px;
  flex-shrink: 0;
}

@media (max-width: 768px) {
  .product-sidebar {
    position: static;
  }
}
</style>
