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

    <div class="service-item__meta">
      <Badge variant="neutral" size="sm" icon="file" label="نوع خدمت :" :value="typeLabel" />
      <Badge variant="neutral" size="sm" icon="calendar" label="تاریخ پیام :" :value="createdAt" />
      <Badge
        :variant="statusVariant"
        size="sm"
        dot
        clickable
        :value="statusLabel"
        @click="onStatusClick"
      />
    </div>

    <div class="service-item__actions">
      <BaseButton
        text="جزئیات درخواست"
        custom-class="service-item__details-btn"
        @click="emit('show-details', service)"
      >
        <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 Badge from '@/components/Badge.vue'
import BaseButton from '@/components/BaseButton.vue'
import SvgIcon from '@/components/icons/SvgIcon.vue'
import { SERVICE_TYPE, TICKET_STATUS } from '@/enums'
import { formatJalaaliDate } from '@/utils/date-utils'
import DropdownMenu from '@/components/DropdownMenu.vue'

const STATUS_VARIANT = {
  open: 'warning',
  answered: 'primary',
  closed: 'info',
  approved: 'success',
  pending: 'warning',
  in_progress: 'primary',
  completed: 'info',
}

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

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

const userName = computed(() => {
  const u = props.service.user
  if (!u) return '—'
  return `${u.firstName || ''} ${u.lastName || ''}`.trim() || u.fullName || '—'
})

const typeLabel = computed(() => props.service.typeLabel || SERVICE_TYPE[props.service.type] || '—')

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

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

const createdAt = computed(() => {
  if (props.service.faCreatedAt) {
    return props.service.faCreatedTime
      ? `${props.service.faCreatedTime}، ${props.service.faCreatedAt}`
      : props.service.faCreatedAt
  }
  return formatJalaaliDate(props.service.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.service.status)
    .map(([value, label]) => ({
      key: `status-${value}`,
      label: `تغییر به ${label}`,
      icon: 'arrows-clockwise',
      onClick: () => emit('change-status', { service: props.service, status: value }),
    }))
)
</script>

<style lang="scss" scoped>
.service-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>
