<template>
  <div class="ses">
    <div class="ses__info">
      <p class="ses__title">{{ exam.title || '—' }}</p>

      <div class="ses__stats">
        <div class="ses__stat">
          <span class="ses__stat-label">تعداد سوال :</span>
          <span class="ses__stat-value">{{ questionsCountText }}</span>
        </div>
        <div class="ses__stat">
          <SvgIcon name="users-three" :size="12" color="#5d5d5d" />
          <span class="ses__stat-label">نمره قبولی :</span>
          <span class="ses__stat-value">{{ passingScoreText }}</span>
        </div>
        <div class="ses__stat ses__stat--pill">
          <SvgIcon name="calendar" :size="11" color="#535353" />
          <span class="ses__stat-label">مدت آزمون :</span>
          <span class="ses__stat-value">{{ durationText }}</span>
        </div>
        <div class="ses__status" :class="`ses__status--${statusTone}`">
          <span class="ses__status-label">وضعیت :</span>
          <span class="ses__status-value">{{ statusLabel }}</span>
        </div>
      </div>
    </div>

    <div class="ses__action">
      <BaseButton
        :text="startButtonText"
        :loading="starting"
        custom-class="ses__start-btn"
        @click="emit('start')"
      >
        <template #appendIcon>
          <SvgIcon name="arrow-left" :size="18" color="#fff" />
        </template>
      </BaseButton>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import BaseButton from '@/components/BaseButton.vue'
import SvgIcon from '@/components/icons/SvgIcon.vue'

const TONE_MAP = {
  passed: 'success',
  failed: 'danger',
  in_progress: 'success',
  not_started: 'neutral',
}

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

const emit = defineEmits(['start'])

const questionsCountText = computed(() =>
  props.exam.questionsCount == null ? '—' : `${props.exam.questionsCount}`
)
const passingScoreText = computed(() =>
  props.exam.passingScore == null ? '—' : `${props.exam.passingScore}`
)
const durationText = computed(() =>
  props.exam.durationMinutes == null ? '—' : `${props.exam.durationMinutes} دقیقه`
)

const statusLabel = computed(() => props.exam.statusLabel || '—')
const statusTone = computed(() => TONE_MAP[props.exam.status] || 'neutral')

const startButtonText = computed(() =>
  (props.exam.attempts || []).length > 0 ? 'شروع آزمون مجدد' : 'شروع آزمون'
)
</script>

<style lang="scss" scoped>
.ses {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 1rem;
  padding: 1.25rem;
  background: rgba(255, 255, 255, 40%);
  box-shadow: 0 6.75px 19.425px rgba(0, 0, 0, 4%);
  border-radius: 0.75rem;
  margin-bottom: 0.75rem;

  @media (min-width: 1024px) {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 1.25rem 2rem;
    min-height: 9.9rem;
  }

  &__info {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 1rem;
    text-align: end;
  }

  &__title {
    font-family: var(--font-family-fa);
    font-weight: 400;
    font-size: 1.5rem;
    color: #4b4b4b;
    margin: 0;
    line-height: 1.246;
  }

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

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

    &--pill {
      border-radius: 1.375rem;
    }
  }

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

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

    &--neutral {
      background: rgba(107, 107, 107, 4%);
      color: #535353;
    }
  }

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

  &__status-value {
    font-weight: 500;
    font-size: 0.75rem;
  }

  &__action {
    display: flex;
    justify-content: center;

    @media (min-width: 1024px) {
      justify-content: flex-start;
    }
  }

  &__start-btn {
    min-width: 12rem;
    padding: 0 2rem;
    height: 2rem;
  }
}
</style>
