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

    <div class="course-item__meta">
      <Badge v-if="course.term?.title" label="مختص به:" :value="course.term.title" />
      <Badge label="ظرفیت:" :value="capacity || '—'" />
      <Badge
        v-if="course.prerequisitesCount"
        label="پیش‌نیاز:"
        :value="course.prerequisitesCount"
      />
    </div>

    <div v-if="!isActive" class="course-item__status">
      <span class="course-item__status-badge">
        <span class="course-item__status-dot" />
        غیرفعال
      </span>
    </div>

    <div class="course-item__actions">
      <ToggleSwitch
        :model-value="!!isActive"
        @update:model-value="emit('change-status', { id: course.id, isActive: !isActive })"
      />
      <CircleButton
        tooltip="حذف"
        bg-color="rgba(104, 104, 104, 0.05)"
        size="2.5rem"
        @click="emit('delete', course)"
      >
        <template #icon>
          <SvgIcon name="trash" :size="18" color="var(--color-error)" />
        </template>
      </CircleButton>
      <CircleButton
        tooltip="ویرایش"
        bg-color="rgba(104, 104, 104, 0.05)"
        size="2.5rem"
        @click="emit('edit', course)"
      >
        <template #icon>
          <SvgIcon name="pencil" :size="18" color="var(--color-sec-gray)" />
        </template>
      </CircleButton>
      <BaseButton
        text="جزئیات دوره"
        custom-class="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 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 ToggleSwitch from '@/components/form/ToggleSwitch.vue'

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

const emit = defineEmits(['edit', 'delete', 'change-status', 'show-details'])

const teacherName = computed(() => {
  const t = props.course.teacher
  if (!t) return '—'
  return t.name
})

const capacity = computed(() => props.course.capacity ?? '')

const isActive = computed(() => props.course.isActive ?? false)
</script>

<style lang="scss" scoped>
.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-start;
  }

  &__status {
    flex: 0 0 auto;
  }

  &__status-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.25rem 1rem;
    border-radius: 0.875rem;
    background: rgba(204, 40, 49, 6%);
    color: var(--color-error);
    font-family: var(--font-family-fa);
    font-size: 0.7rem;
  }

  &__status-dot {
    width: 0.375rem;
    height: 0.375rem;
    border-radius: 9999px;
    background: currentcolor;
  }

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

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

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