<template>
  <div class="ticket-item" :class="{ 'ticket-item--clickable': isMobile }" @click="onItemClick">
    <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__request-id">
          <span class="ticket-item__request-id-label">شماره درخواست :</span>
          <span class="ticket-item__request-id-value">{{ ticket.id || '—' }}</span>
        </p>
      </div>
    </div>

    <div class="ticket-item__meta">
      <Badge variant="neutral" size="sm" icon="calendar" label="تاریخ ثبت :" :value="createdAt" />
      <Badge
        v-if="priorityLabel"
        :variant="priorityVariant"
        size="sm"
        label="اولویت :"
        :value="priorityLabel"
      />
      <Badge :variant="statusVariant" size="sm" dot :value="statusLabel" />
    </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 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 { TICKET_PRIORITY, TICKET_STATUS } from '@/enums'
import { computed, onBeforeUnmount, onMounted, ref } from 'vue'

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

const PRIORITY_VARIANT = {
  low: 'neutral',
  normal: 'info',
  high: 'warning',
  urgent: 'danger',
}

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

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

const isMobile = ref(window.innerWidth < 1024)
const onResize = () => {
  isMobile.value = window.innerWidth < 1024
}
onMounted(() => window.addEventListener('resize', onResize))
onBeforeUnmount(() => window.removeEventListener('resize', onResize))

const onItemClick = () => {
  if (isMobile.value) emit('show-details', props.ticket)
}

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

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

const priorityLabel = computed(() => TICKET_PRIORITY[props.ticket.priority] || '')
const priorityVariant = computed(() => PRIORITY_VARIANT[props.ticket.priority] || 'neutral')

const createdAt = computed(() => formatJalaaliDate(props.ticket.createdAt) || '—')
</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;
  }

  &--clickable {
    cursor: pointer;
  }

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

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

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

    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: none;
    justify-content: flex-end;
    align-items: center;
    gap: 0.375rem;
    flex: 1 1 100%;

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

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