<template>
  <div class="verification-item">
    <div class="verification-item__user">
      <div v-if="user.avatarUrl" class="verification-item__avatar">
        <img :src="user.avatarUrl" :alt="fullName" />
      </div>
      <div v-else class="verification-item__avatar verification-item__avatar--placeholder">
        <SvgIcon name="user" :size="24" color="#bcbcbc" />
      </div>
      <div class="verification-item__info">
        <p class="verification-item__name">{{ fullName }}</p>
        <p class="verification-item__location">
          <span>{{ user?.province?.name || '—' }}</span>
          <span class="verification-item__sep">،</span>
          <span>{{ user?.city?.name || '—' }}</span>
        </p>
      </div>
    </div>

    <div class="verification-item__meta">
      <div class="verification-item__date">
        <span class="verification-item__date-label">تاریخ ثبت نام:</span>
        <span class="verification-item__date-value">
          {{ formatJalaaliDateTime(user.createdAt) || '—' }}
        </span>
      </div>
      <Badge
        v-if="user.status === 'rejected'"
        variant="danger"
        :dot="true"
        value="رد شده"
        :clickable="true"
        @click="emit('show-reject-reason', user)"
      />
      <Badge
        v-else-if="statusLabel"
        :variant="statusVariant"
        :dot="true"
        :value="statusLabel"
        :clickable="user.status === 'blocked'"
        @click="emit('show-reject-reason', user)"
      />
    </div>

    <div class="verification-item__actions">
      <CircleButton
        tooltip="تایید"
        bg-color="rgba(0, 154, 18, 0.08)"
        size="2.5rem"
        @click="emit('verify-user', { id: user.id, status: 'approved' })"
      >
        <template #icon>
          <SvgIcon name="check" :size="20" color="var(--color-secondary)" />
        </template>
      </CircleButton>
      <CircleButton
        v-if="user.status !== 'blocked'"
        tooltip="عدم تایید"
        bg-color="rgba(204, 40, 49, 0.08)"
        size="2.5rem"
        @click="emit('verify-user', { id: user.id, status: 'blocked' })"
      >
        <template #icon>
          <SvgIcon name="close" :size="20" color="var(--color-error)" />
        </template>
      </CircleButton>
      <BaseButton
        custom-class="verification-item__details-btn"
        text="جزئیات احراز هویت"
        :loading="detailsLoading"
        @click="emit('show-details', user.id)"
      >
        <template #appendIcon>
          <SvgIcon name="caret-left" :size="16" color="#fff" />
        </template>
      </BaseButton>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import { USER_STATUS } from '@/enums'
import Badge from '@/components/Badge.vue'
import BaseButton from '@/components/BaseButton.vue'
import SvgIcon from '@/components/icons/SvgIcon.vue'
import CircleButton from '@/components/CircleButton.vue'
import { formatJalaaliDateTime } from '@/utils/date-utils'

const props = defineProps({
  user: { type: Object, required: true },
  detailsLoading: { type: Boolean, default: false },
})

const emit = defineEmits(['show-details', 'show-reject-reason', 'verify-user'])

const STATUS_VARIANTS = {
  pending: 'neutral',
  verified: 'warning',
  approved: 'success',
  blocked: 'danger',
}

const fullName = computed(() => `${props.user.name || ''}`.trim() || '—')
const statusLabel = computed(() => USER_STATUS[props.user.status] || '')
const statusVariant = computed(() => STATUS_VARIANTS[props.user.status] || 'neutral')
</script>

<style lang="scss" scoped>
.verification-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: 768px) {
    flex-direction: row;
    align-items: center;
    gap: 0.625rem;
  }

  &__user {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    flex: 1 1 33%;
  }

  &__avatar {
    height: 3rem;
    width: 3rem;
    border-radius: 9999px;
    overflow: hidden;
    border: 1px solid #eee;
    flex-shrink: 0;

    img {
      height: 100%;
      width: 100%;
      object-fit: cover;
    }

    &--placeholder {
      background: #f5f5f5;
      display: flex;
      align-items: center;
      justify-content: center;
      border: none;
    }
  }

  &__info {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    min-width: 0;
  }

  &__name {
    font-family: var(--font-family-fa);
    font-weight: 400;
    font-size: 1rem;
    color: #4b4b4b;
    margin: 0;
    white-space: nowrap;
  }

  &__location {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    font-family: var(--font-family-fa);
    font-size: 0.75rem;
    font-weight: 300;
    color: #848484;
    margin: 0;
    white-space: nowrap;
  }

  &__sep {
    color: #c4c4c4;
  }

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

  &__date {
    background: rgba(107, 107, 107, 5%);
    padding: 0.25rem 1rem;
    border-radius: 0.875rem;
    white-space: nowrap;
    line-height: 1.5;
  }

  &__date-label {
    font-family: var(--font-family-fa);
    font-weight: 300;
    font-size: 0.75rem;
    color: #848484;
    margin-inline-end: 0.25rem;
  }

  &__date-value {
    font-family: var(--font-family-en);
    font-size: 0.75rem;
  }

  &__actions {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    gap: 0.375rem;
    flex: 1 1 33%;
  }

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