<template>
  <div class="student-exam-attempt">
    <p class="student-exam-attempt__title">{{ attempt.title || 'آزمون' }}</p>

    <div class="student-exam-attempt__meta">
      <div class="student-exam-attempt__stat">
        <SvgIcon name="scroll" :size="11" color="#5d5d5d" />
        <span class="student-exam-attempt__stat-label">نمره :</span>
        <span class="student-exam-attempt__stat-value">{{ scoreText }}</span>
      </div>
      <div class="student-exam-attempt__stat">
        <SvgIcon name="users-three" :size="11" color="#5d5d5d" />
        <span class="student-exam-attempt__stat-label">زمان آزمون :</span>
        <span class="student-exam-attempt__stat-value">{{ dateText }}</span>
      </div>
      <div class="student-exam-attempt__status" :class="`student-exam-attempt__status--${tone}`">
        <SvgIcon name="pencil" :size="11" :color="toneColor" />
        <span class="student-exam-attempt__status-label">وضعیت :</span>
        <span class="student-exam-attempt__status-value">{{ statusLabel }}</span>
      </div>
    </div>
  </div>
</template>

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

const TONE_MAP = { passed: 'success', failed: 'danger' }
const TONE_COLOR = { success: '#007074', danger: '#740002' }

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

const scoreText = computed(() => (props.attempt.score == null ? '—' : `${props.attempt.score}`))
const dateText = computed(() => {
  if (props.attempt.faTime && props.attempt.faDate)
    return `${props.attempt.faTime}  ${props.attempt.faDate}`
  if (props.attempt.faDate) return props.attempt.faDate
  return formatJalaaliDate(props.attempt.date) || '—'
})
const tone = computed(() => TONE_MAP[props.attempt.status] || 'success')
const toneColor = computed(() => TONE_COLOR[tone.value] || '#5d5d5d')
const statusLabel = computed(
  () => props.attempt.statusLabel || (tone.value === 'success' ? 'قبول شده' : 'قبول نشده')
)
</script>

<style lang="scss" scoped>
.student-exam-attempt {
  display: flex;
  flex-direction: column-reverse;
  gap: 0.625rem;
  padding: 0.625rem 1.25rem;
  background: rgba(255, 255, 255, 58%);
  box-shadow: 0 6.75px 19.425px rgba(0, 0, 0, 4%);
  border-radius: 0.75rem;
  margin-bottom: 0.625rem;

  @media (min-width: 1024px) {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
  }

  &__title {
    font-family: var(--font-family-fa);
    font-weight: 500;
    font-size: 0.95rem;
    color: #4b4b4b;
    margin: 0;
    text-align: end;
    flex: 0 0 auto;
    order: 4;

    @media (min-width: 1024px) {
      order: 0;
    }
  }

  &__meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.5rem;
    flex: 1;
    justify-content: center;
  }

  &__stat {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.375rem 0.875rem;
    border-radius: 0.75rem;
    background: rgba(107, 107, 107, 4%);
    font-family: var(--font-family-fa);
    white-space: nowrap;
  }

  &__stat-label {
    font-weight: 400;
    font-size: 0.65rem;
    color: #5d5d5d;
  }

  &__stat-value {
    font-family: var(--font-family-en);
    font-weight: 500;
    font-size: 0.75rem;
    color: #5d5d5d;
  }

  &__status {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.375rem 0.875rem;
    border-radius: 0.75rem;
    font-family: var(--font-family-fa);
    white-space: nowrap;

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

    &--danger {
      background: rgba(116, 0, 2, 4%);
      color: #740002;
    }
  }

  &__status-label {
    font-weight: 400;
    font-size: 0.65rem;
  }

  &__status-value {
    font-weight: 500;
    font-size: 0.75rem;
  }
}
</style>
