<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">
      <div class="service-item__pill">
        <SvgIcon name="file" :size="14" color="#535353" />
        <span class="service-item__pill-label">نوع خدمت :</span>
        <span class="service-item__pill-value">{{ typeLabel }}</span>
      </div>
      <div class="service-item__pill">
        <SvgIcon name="calendar" :size="14" color="#535353" />
        <span class="service-item__pill-label">تاریخ پیام :</span>
        <span class="service-item__pill-value">{{ createdAt }}</span>
      </div>
      <button
        type="button"
        class="service-item__status"
        :class="`service-item__status--${service.status || 'pending'}`"
        @click="onStatusClick"
      >
        <span class="service-item__status-dot" />
        <span>{{ statusLabel }}</span>
      </button>
    </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 BaseButton from '@/components/BaseButton.vue'
import SvgIcon from '@/components/icons/SvgIcon.vue'
import { SERVICE_STATUS, SERVICE_TYPE } from '@/enums'
import { formatJalaaliDate } from '@/utils/date-utils'
import DropdownMenu from '@/components/DropdownMenu.vue'

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 || SERVICE_STATUS[props.service.status] || '—'
)

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(SERVICE_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%;
  }

  &__pill {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    background: rgba(107, 107, 107, 5%);
    padding: 0.375rem 0.875rem;
    border-radius: 0.875rem;
    line-height: 1.5;
    white-space: nowrap;
  }

  &__pill-label {
    font-family: var(--font-family-fa);
    font-weight: 300;
    font-size: 0.7rem;
    color: #535353;
  }

  &__pill-value {
    font-family: var(--font-family-en);
    font-size: 0.7rem;
    font-weight: 500;
    color: #535353;
  }

  &__status {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.375rem 0.875rem;
    border-radius: 0.875rem;
    border: none;
    font-family: var(--font-family-fa);
    font-size: 0.7rem;
    font-weight: 500;
    white-space: nowrap;
    cursor: pointer;
    transition: filter 0.15s ease;

    &:hover {
      filter: brightness(0.96);
    }

    &--approved {
      background: rgba(0, 153, 76, 10%);
      color: #00994c;
    }

    &--pending {
      background: rgba(204, 154, 40, 10%);
      color: #cc6f00;
    }

    &--in_progress {
      background: rgba(0, 112, 116, 10%);
      color: #007074;
    }

    &--completed {
      background: rgba(104, 104, 104, 10%);
      color: #686868;
    }
  }

  &__status-dot {
    width: 0.45rem;
    height: 0.45rem;
    border-radius: 9999px;
    background: currentcolor;
  }

  &__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>
