<template>
  <div class="student-course-item">
    <div class="student-course-item__main">
      <div v-if="course.image" class="student-course-item__image">
        <img :src="course.image" :alt="course.title" />
      </div>
      <div v-else class="student-course-item__image student-course-item__image--placeholder">
        <SvgIcon name="book" :size="22" color="#bcbcbc" />
      </div>
      <div class="student-course-item__title-block">
        <p class="student-course-item__title">
          <span>دوره</span>
          <strong>{{ course.title }}</strong>
        </p>
        <div class="student-course-item__teacher">
          <SvgIcon name="user" :size="11" color="#bcbcbc" />
          <span class="student-course-item__teacher-label">استاد:</span>
          <span class="student-course-item__teacher-name">{{ teacherName }}</span>
        </div>
      </div>
    </div>

    <div class="student-course-item__meta">
      <div v-if="course.term?.title" class="student-course-item__pill">
        <span class="student-course-item__pill-label">ترم:</span>
        <span class="student-course-item__pill-value">{{ course.term.title }}</span>
      </div>
      <div class="student-course-item__pill">
        <span class="student-course-item__pill-label">تاریخ شروع:</span>
        <span class="student-course-item__pill-value">{{ startDate }}</span>
      </div>
      <div class="student-course-item__pill">
        <span class="student-course-item__pill-label">تاریخ پایان:</span>
        <span class="student-course-item__pill-value">{{ endDate }}</span>
      </div>
    </div>

    <div v-if="statusLabel" class="student-course-item__status">
      <span
        class="student-course-item__status-badge"
        :class="`student-course-item__status-badge--${course.status || 'neutral'}`"
      >
        {{ statusLabel }}
      </span>
    </div>

    <div class="student-course-item__actions">
      <BaseButton
        text="مشاهده دوره"
        custom-class="student-course-item__details-btn"
        @click="emit('show-details', course)"
      >
        <template #appendIcon>
          <SvgIcon name="caret-left" :size="16" color="#fff" />
        </template>
      </BaseButton>
    </div>
  </div>
</template>

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

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

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

const teacherName = computed(() => {
  const t = props.course.teacher
  if (!t) return '—'
  return `${t.firstName || ''} ${t.lastName || ''}`.trim() || t.fullName || '—'
})

const startDate = computed(
  () => props.course.faStartDate || formatJalaaliDate(props.course.startDate) || '—'
)

const endDate = computed(
  () => props.course.faEndDate || formatJalaaliDate(props.course.endDate) || '—'
)

const statusLabel = computed(
  () => STUDENT_COURSE_STATUS[props.course.status] || props.course.statusLabel || ''
)
</script>

<style lang="scss" scoped>
.student-course-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: 1280px) {
    flex-flow: row wrap;
    align-items: center;
  }

  &__main {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    flex: 1 1 33%;
    min-width: 0;
  }

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

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

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

  &__title-block {
    min-width: 0;
  }

  &__title {
    font-family: var(--font-family-fa);
    font-size: 0.95rem;
    color: #4b4b4b;
    margin: 0;
    display: flex;
    gap: 0.25rem;

    strong {
      font-weight: 700;
    }
  }

  &__teacher {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    margin-top: 0.25rem;
  }

  &__teacher-label {
    font-family: var(--font-family-fa);
    font-size: 0.65rem;
    color: #838383;
  }

  &__teacher-name {
    font-family: var(--font-family-fa);
    font-size: 0.75rem;
    color: #4b4b4b;
  }

  &__meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
    flex: 1 1 33%;
    justify-content: flex-end;
  }

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

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

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

  &__status {
    flex: 0 0 auto;
  }

  &__status-badge {
    display: inline-flex;
    align-items: center;
    padding: 0.25rem 1rem;
    border-radius: 0.875rem;
    font-family: var(--font-family-fa);
    font-size: 0.7rem;

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

    &--completed {
      background: rgba(0, 154, 18, 8%);
      color: #009a12;
    }

    &--neutral {
      background: rgba(180, 180, 180, 8%);
      color: #686868;
    }
  }

  &__actions {
    display: flex;
    justify-content: flex-end;
    align-items: center;
    flex: 1 1 100%;

    @media (min-width: 1280px) {
      flex: 1 1 auto;
    }
  }

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