<script setup lang="ts">
import { storeToRefs } from 'pinia'
import { onMounted } from 'vue'
import SiteHeader from '@/components/SiteHeader.vue'
import { useMediaSetsStore } from '@/stores/mediaSets'

const mediaSetsStore = useMediaSetsStore()
const { sets, isLoading, error } = storeToRefs(mediaSetsStore)

onMounted(() => {
  void mediaSetsStore.fetchSets()
})
</script>

<template>
  <div class="media-page">
    <SiteHeader />
    <main class="media-page__main" data-cy="media-page">
      <section class="media-page__hero">
        <p class="media-page__eyebrow">Rabbi Yehoshua Gerzi</p>
        <h1 class="media-page__heading">Torah Media</h1>
        <p class="media-page__intro">
          Explore Rabbi Gerzi's Torah videos, audio, and written resources.
        </p>
      </section>

      <section class="media-page__sets" aria-label="Featured sets">
        <div class="media-page__sets-header">
          <p class="media-page__eyebrow">Collections</p>
          <h2 class="media-page__section-heading">Featured Sets</h2>
        </div>

        <p v-if="isLoading" class="media-page__status">Loading media sets...</p>
        <p
          v-else-if="error"
          class="media-page__status media-page__status--error"
          data-cy="media-sets-error"
        >
          {{ error }}
        </p>
        <p v-else-if="sets.length === 0" class="media-page__status">
          No media sets are available yet.
        </p>
        <div v-else class="media-page__grid">
          <article
            v-for="mediaSet in sets"
            :key="mediaSet.id"
            class="media-page__card"
            data-cy="media-set-card"
          >
            <p class="media-page__card-kicker">Set</p>
            <h3 class="media-page__card-title">{{ mediaSet.name }}</h3>
          </article>
        </div>
      </section>
    </main>
  </div>
</template>

<style scoped>
.media-page {
  min-height: 100vh;
  background: var(--color-cream);
}

.media-page__main {
  padding: 5rem 2rem 6rem;
}

.media-page__hero {
  max-width: 760px;
  margin: 0 auto;
  padding: 4rem 3rem;
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-radius: 14px;
  text-align: center;
}

.media-page__eyebrow {
  margin: 0 0 1rem;
  color: var(--color-olive);
  font-size: 0.9rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.media-page__heading {
  margin: 0;
  color: var(--color-slate);
  font-family: var(--font-serif);
  font-size: clamp(2.25rem, 5vw, 4rem);
  font-weight: 400;
  line-height: 1.1;
}

.media-page__intro {
  max-width: 34rem;
  margin: 1.5rem auto 0;
  color: var(--color-text-muted);
  font-family: var(--font-serif);
  font-size: 1.1rem;
  line-height: 1.7;
}

.media-page__sets {
  max-width: 1100px;
  margin: 3rem auto 0;
}

.media-page__sets-header {
  margin-bottom: 1.5rem;
}

.media-page__section-heading {
  margin: 0;
  color: var(--color-slate);
  font-family: var(--font-serif);
  font-size: clamp(1.8rem, 3vw, 2.6rem);
  font-weight: 400;
  line-height: 1.2;
}

.media-page__status {
  margin: 0;
  padding: 1rem 1.25rem;
  color: var(--color-text-muted);
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  font-size: 0.95rem;
}

.media-page__status--error {
  color: #7c2d2d;
  border-color: #e5b8b8;
}

.media-page__grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 1rem;
}

.media-page__card {
  min-height: 170px;
  padding: 1.5rem;
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-top: 4px solid var(--color-olive);
  border-radius: 8px;
}

.media-page__card-kicker {
  margin: 0 0 0.8rem;
  color: var(--color-olive);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.media-page__card-title {
  margin: 0;
  color: var(--color-slate-dark);
  font-family: var(--font-serif);
  font-size: 1.45rem;
  font-weight: 400;
  line-height: 1.35;
}

@media (max-width: 768px) {
  .media-page__main {
    padding: 3rem 1rem 4rem;
  }

  .media-page__hero {
    padding: 3rem 1.5rem;
  }

  .media-page__sets {
    margin-top: 2rem;
  }
}
</style>
