<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">
      <header class="media-page__header">
        <h1 class="media-page__heading">Torah Media</h1>
      </header>

      <section class="media-page__sets" aria-label="Featured sets">
        <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" data-cy="media-set-grid">
          <template v-for="mediaSet in sets" :key="mediaSet.id">
            <RouterLink
              v-if="mediaSet.rootElementId !== null"
              :to="{ name: 'element', params: { id: mediaSet.rootElementId } }"
              class="media-page__card"
              data-cy="media-set-card"
            >
              <img
                :src="mediaSet.iconImageUrl"
                :alt="`${mediaSet.name} icon`"
                class="media-page__card-icon"
                data-cy="media-set-icon"
              />
              <h2 class="media-page__card-title">{{ mediaSet.name }}</h2>
              <p class="media-page__card-description">
                {{ mediaSet.description }}
              </p>
            </RouterLink>
            <article
              v-else
              class="media-page__card media-page__card--disabled"
              data-cy="media-set-card"
            >
              <img
                :src="mediaSet.iconImageUrl"
                :alt="`${mediaSet.name} icon`"
                class="media-page__card-icon"
                data-cy="media-set-icon"
              />
              <h2 class="media-page__card-title">{{ mediaSet.name }}</h2>
              <p class="media-page__card-description">
                {{ mediaSet.description }}
              </p>
            </article>
          </template>
        </div>
      </section>
    </main>
  </div>
</template>

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

.media-page__main {
  min-height: 100vh;
  padding: 5.1rem 4.15rem 5rem;
}

.media-page__header {
  text-align: center;
}

.media-page__heading {
  margin: 0;
  color: #2c2c2c;
  font-family: var(--font-serif);
  font-size: clamp(2.6rem, 4.8vw, 3.2rem);
  font-weight: 400;
  line-height: 1.1;
}

.media-page__sets {
  margin-top: 5rem;
}

.media-page__status {
  max-width: 48rem;
  margin: 0 auto;
  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;
  max-width: 1745px;
  margin: 0 auto;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 2.7rem 3.75rem;
}

.media-page__card {
  min-height: 448px;
  padding: 3.1rem 3rem 2.7rem;
  background: var(--color-white);
  border: 1px solid #e5cf9f;
  border-radius: 17px;
  color: inherit;
  text-align: center;
  text-decoration: none;
  transition:
    border-color 180ms ease,
    box-shadow 180ms ease,
    transform 180ms ease;
}

a.media-page__card:hover,
a.media-page__card:focus-visible {
  border-color: #d4ad5f;
  box-shadow: 0 14px 35px rgb(44 44 44 / 10%);
  transform: translateY(-2px);
}

a.media-page__card:focus-visible {
  outline: 3px solid rgb(212 173 95 / 45%);
  outline-offset: 4px;
}

.media-page__card--disabled {
  opacity: 0.72;
}

.media-page__card-icon {
  display: block;
  width: 140px;
  height: 140px;
  margin: 0 auto 2.65rem;
  object-fit: contain;
}

.media-page__card-title {
  margin: 0;
  color: #333333;
  font-family: var(--font-serif);
  font-size: clamp(2rem, 3.7vw, 2.4rem);
  font-weight: 400;
  line-height: 1.2;
}

.media-page__card-description {
  max-width: 35rem;
  margin: 2.15rem auto 0;
  color: #333333;
  font-family: var(--font-serif);
  font-size: clamp(1.25rem, 2.3vw, 1.45rem);
  line-height: 1.38;
}

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

  .media-page__sets {
    margin-top: 3rem;
  }

  .media-page__grid {
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .media-page__card {
    min-height: 390px;
    padding: 2.5rem 1.5rem;
  }

  .media-page__card-icon {
    width: 118px;
    height: 118px;
    margin-bottom: 2rem;
  }
}
</style>
