<template>
  <div class="ticket-item">
    <div class="ticket-item__user">
      <div v-if="ticket.student?.avatarUrl" class="ticket-item__avatar">
        <img :src="ticket.student.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.subject || '—' }}</p>
      </div>
    </div>

    <div class="ticket-item__meta">
      <Badge :variant="statusVariant" size="sm" :value="statusLabel" />
      <Badge variant="neutral" size="sm" label="تاریخ ثبت:" :value="createdAt" />
      <Badge v-if="createdTime" variant="neutral" size="sm" label="ساعت:" :value="createdTime" />
    </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 Badge from '@/components/Badge.vue'
import BaseButton from '@/components/BaseButton.vue'
import SvgIcon from '@/components/icons/SvgIcon.vue'
import { formatJalaaliDate } from '@/utils/date-utils'

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

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

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

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

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

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

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

const createdTime = computed(() => {
  const iso = props.ticket.createdAt
  if (!iso) return ''
  const d = new Date(iso)
  if (Number.isNaN(d.getTime())) return ''
  return d.toLocaleTimeString('fa-IR', { hour: '2-digit', minute: '2-digit' })
})
</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%;
  }

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