<template>
  <div class="ticket-item">
    <div class="ticket-item__user">
      <div v-if="ticket.user?.avatarUrl" class="ticket-item__avatar">
        <img :src="ticket.user.avatarUrl" :alt="userName" />
      </div>
      <div v-else class="ticket-item__avatar ticket-item__avatar--placeholder">
        <SvgIcon name="user" :size="24" color="#bcbcbc" />
      </div>
      <div class="ticket-item__info">
        <p class="ticket-item__name">{{ userName }}</p>
        <p class="ticket-item__title">{{ ticket.title || '—' }}</p>
      </div>
    </div>

    <div class="ticket-item__meta">
      <span
        class="ticket-item__status"
        :class="`ticket-item__status--${ticket.status || 'pending'}`"
      >
        {{ statusLabel }}
      </span>
      <div class="ticket-item__pill">
        <span class="ticket-item__pill-label">تاریخ ثبت:</span>
        <span class="ticket-item__pill-value">{{ createdAt }}</span>
      </div>
      <div v-if="createdTime" class="ticket-item__pill">
        <span class="ticket-item__pill-label">ساعت:</span>
        <span class="ticket-item__pill-value">{{ createdTime }}</span>
      </div>
    </div>

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

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

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

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

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

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

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

const createdTime = computed(() => props.ticket.faCreatedTime || '')
</script>

<style lang="scss" scoped>
.ticket-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 33%;
    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;
  }

  &__title {
    font-family: var(--font-family-fa);
    font-size: 0.7rem;
    font-weight: 300;
    color: #8f8f8f;
    margin: 0;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  &__meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.375rem;
    flex: 1 1 41%;
  }

  &__status {
    padding: 0.25rem 0.875rem;
    border-radius: 0.875rem;
    font-family: var(--font-family-fa);
    font-size: 0.7rem;
    font-weight: 500;
    white-space: nowrap;

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

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

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

  &__pill {
    background: rgba(107, 107, 107, 5%);
    padding: 0.25rem 1rem;
    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;
    margin-inline-end: 0.25rem;
  }

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

  &__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: 8rem;
    padding: 0 0.875rem;
  }
}
</style>
