<template>
  <div class="consultation-item">
    <div class="consultation-item__user">
      <div v-if="consultation.student?.avatarUrl" class="consultation-item__avatar">
        <img :src="consultation.student.avatarUrl" :alt="userName" />
      </div>
      <div v-else class="consultation-item__avatar consultation-item__avatar--placeholder">
        <SvgIcon name="user" :size="24" color="#bcbcbc" />
      </div>
      <div class="consultation-item__info">
        <p class="consultation-item__name">{{ userName }}</p>
        <p class="consultation-item__request-id">
          <span class="consultation-item__request-id-label">شماره درخواست :</span>
          <span class="consultation-item__request-id-value">{{ consultation.id || '—' }}</span>
        </p>
      </div>
    </div>

    <div class="consultation-item__meta">
      <Badge variant="neutral" size="sm" label="تاریخ پیام :" :value="createdAt" />
      <Badge
        :variant="statusVariant"
        size="sm"
        dot
        clickable
        :value="statusLabel"
        @click="onStatusClick"
      />
    </div>

    <div class="consultation-item__actions">
      <BaseButton
        text="جزئیات مشاوره"
        custom-class="consultation-item__details-btn"
        @click="emit('show-details', consultation)"
      >
        <template #appendIcon>
          <SvgIcon name="caret-left" :size="16" color="#fff" />
        </template>
      </BaseButton>
    </div>

    <DropdownMenu
      v-model="menuOpen"
      :reference="menuTrigger"
      :items="menuItems"
      placement="bottom-end"
    />
  </div>
</template>

<script setup>
import { computed, ref } from 'vue'
import { TICKET_STATUS } from '@/enums'
import Badge from '@/components/Badge.vue'
import BaseButton from '@/components/BaseButton.vue'
import SvgIcon from '@/components/icons/SvgIcon.vue'
import { formatJalaaliDate } from '@/utils/date-utils'
import DropdownMenu from '@/components/DropdownMenu.vue'

const STATUS_VARIANT = {
  open: 'primary',
  answered: 'success',
  closed: 'info',
}

const props = defineProps({
  consultation: { type: Object, required: true },
})

const emit = defineEmits(['show-details', 'change-status'])

const userName = computed(() => props.consultation.student?.name || '—')

const statusLabel = computed(() => TICKET_STATUS[props.consultation.status] || '—')

const statusVariant = computed(() => STATUS_VARIANT[props.consultation.status] || 'neutral')

const createdAt = computed(() => formatJalaaliDate(props.consultation.createdAt) || '—')

const menuOpen = ref(false)
const menuTrigger = ref(null)

const onStatusClick = (event) => {
  const trigger = event.currentTarget
  if (menuOpen.value && menuTrigger.value === trigger) {
    menuOpen.value = false
    return
  }
  menuTrigger.value = trigger
  menuOpen.value = true
}

const menuItems = computed(() =>
  Object.entries(TICKET_STATUS)
    .filter(([value]) => value !== props.consultation.status)
    .map(([value, label]) => ({
      key: `status-${value}`,
      label: `تغییر به ${label}`,
      icon: 'arrows-clockwise',
      onClick: () => emit('change-status', { consultation: props.consultation, status: value }),
    }))
)
</script>

<style lang="scss" scoped>
.consultation-item {
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
  padding: 0.75rem;
  background: rgba(255, 255, 255, 50%);
  box-shadow: 0 4px 10px -6px rgba(241, 241, 241, 70%);
  border-radius: 0.875rem;
  margin-bottom: 0.625rem;

  @media (min-width: 1024px) {
    flex-flow: row wrap;
    align-items: center;
  }

  &__user {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex: 1 1 30%;
    min-width: 0;
  }

  &__avatar {
    width: 3rem;
    height: 3rem;
    min-width: 3rem;
    border-radius: 9999px;
    overflow: hidden;
    background: #fff;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;

    img {
      width: 100%;
      height: 100%;
      object-fit: cover;
    }

    &--placeholder {
      background: #f5f5f5;
    }
  }

  &__info {
    min-width: 0;
  }

  &__name {
    font-family: var(--font-family-fa);
    font-size: 0.95rem;
    color: #4b4b4b;
    margin: 0 0 0.25rem;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  &__request-id {
    font-family: var(--font-family-fa);
    font-size: 0.7rem;
    color: #8f8f8f;
    margin: 0;
  }

  &__request-id-label {
    margin-inline-end: 0.25rem;
  }

  &__request-id-value {
    font-family: var(--font-family-en);
    color: #535353;
  }

  &__meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-start;
    gap: 0.375rem;
    flex: 1 1 45%;
  }

  &__actions {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 0.375rem;
    flex: 1 1 100%;

    @media (min-width: 1024px) {
      flex: 0 0 auto;
    }
  }

  &__details-btn {
    min-width: 9rem;
    padding: 0 0.875rem;
  }
}
</style>
