<template>
  <div class="session-item">
    <div class="session-item__main">
      <div v-if="session.image" class="session-item__image">
        <img :src="session.image" :alt="session.title" />
      </div>
      <div v-else class="session-item__image session-item__image--placeholder">
        <SvgIcon name="book" :size="22" color="#bcbcbc" />
      </div>
      <p class="session-item__title">{{ session.title }}</p>
    </div>

    <div class="session-item__meta">
      <div class="session-item__pill">
        <span class="session-item__pill-label">متعلق به دوره:</span>
        <span class="session-item__pill-value">{{ session.course?.title || '—' }}</span>
      </div>
      <div class="session-item__pill">
        <span class="session-item__pill-label">نوع جلسه:</span>
        <span class="session-item__pill-value">{{ sessionTypeLabel }}</span>
      </div>
    </div>

    <div class="session-item__actions">
      <CircleButton
        v-if="hasUsedTerms"
        tooltip="لیست حضور و غیاب"
        bg-color="rgba(0, 112, 116, 0.08)"
        size="2.5rem"
        @click="emit('show-attendance', session)"
      >
        <template #icon>
          <SvgIcon name="users" :size="18" color="#007074" />
        </template>
      </CircleButton>
      <CircleButton
        tooltip="حذف"
        bg-color="rgba(104, 104, 104, 0.05)"
        size="2.5rem"
        @click="emit('delete', session)"
      >
        <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', session)"
      >
        <template #icon>
          <SvgIcon name="pencil" :size="18" color="var(--color-sec-gray)" />
        </template>
      </CircleButton>
      <BaseButton
        text="جزئیات جلسه"
        custom-class="session-item__details-btn"
        @click="emit('show-details', session)"
      >
        <template #appendIcon>
          <SvgIcon name="caret-left" :size="16" color="#fff" />
        </template>
      </BaseButton>
    </div>
  </div>
</template>

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

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

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

const sessionTypeLabel = computed(() => SESSION_TYPE[props.session.type] || '—')

const hasUsedTerms = computed(() => (props.session.usedInTerms?.length ?? 0) > 0)
</script>

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

  &__main {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    flex: 1 1 25%;
    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 {
    font-family: var(--font-family-fa);
    font-size: 1rem;
    color: #4b4b4b;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  &__meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0.375rem;
    flex: 1 1 41%;
  }

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

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

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

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