<template>
  <div>
    <div v-if="pending" class="loading">Loading series details...</div>

    <div v-else-if="error" class="loading">
      <h2>Series Not Found</h2>
      <p>The event series you're looking for doesn't exist.</p>
      <NuxtLink to="/events">&larr; Back to Events</NuxtLink>
    </div>

    <div v-else>
      <!-- BACK LINK -->
      <div class="back-link">
        <NuxtLink to="/events">&larr; Back to Events</NuxtLink>
      </div>

      <!-- SERIES HEADER -->
      <div class="series-header">
        <h1>{{ series.title }}</h1>
        <div class="series-meta-row">
          <span v-if="series.type" class="badge all">{{ formatSeriesType(series.type) }}</span>
          <span class="meta-text">{{ series.events?.length || 0 }} sessions</span>
          <span v-if="series.startDate" class="meta-text">
            {{ formatDate(series.startDate) }} &ndash; {{ formatDate(series.endDate) }}
          </span>
        </div>
      </div>

      <!-- DESCRIPTION -->
      <div v-if="series.description" class="section">
        <p>{{ series.description }}</p>
      </div>

      <!-- EVENT LIST -->
      <div class="section">
        <div class="section-label">Sessions</div>
        <div v-if="series.events?.length">
          <div v-for="(event, index) in series.events" :key="event._id || index" class="event-row">
            <span class="event-num">{{ String(index + 1).padStart(2, '0') }}</span>
            <span class="event-date">{{ formatDate(event.startDate) }}</span>
            <div class="event-info">
              <NuxtLink :to="`/events/${event.slug || event._id || event.id}`" class="event-title-link">
                {{ event.title }}
              </NuxtLink>
              <span class="event-status">{{ getEventStatus(event) }}</span>
            </div>
          </div>
        </div>
        <p v-else class="empty">No sessions scheduled yet.</p>
      </div>

      <!-- PASS PURCHASE -->
      <div v-if="series.passPrice" class="section">
        <DashedBox>
          <div class="section-label">Series Pass</div>
          <p>Get access to all sessions in this series with a single pass.</p>
          <div class="pass-price">${{ series.passPrice }}</div>
        </DashedBox>
      </div>

      <!-- QUESTIONS -->
      <div class="section">
        <div class="section-label">Questions?</div>
        <p>If you have questions about this series, reach out to us.</p>
        <a href="mailto:events@ghostguild.org">events@ghostguild.org</a>
      </div>
    </div>
  </div>
</template>

<script setup>
const route = useRoute()

const { data: series, pending, error } = await useFetch(`/api/series/${route.params.id}`)

if (error.value?.statusCode === 404) {
  throw createError({ statusCode: 404, statusMessage: 'Event series not found' })
}

const formatSeriesType = (type) => {
  const types = { workshop_series: 'Workshop Series', recurring_meetup: 'Recurring Meetup', multi_day: 'Multi-Day Event', course: 'Course', tournament: 'Tournament' }
  return types[type] || type
}

const formatDate = (dateStr) => {
  if (!dateStr) return ''
  return new Date(dateStr).toLocaleDateString('en-US', { month: 'short', day: 'numeric', year: 'numeric' })
}

const getEventStatus = (event) => {
  const now = new Date()
  const start = new Date(event.startDate)
  const end = new Date(event.endDate)
  if (now < start) return 'Upcoming'
  if (now >= start && now <= end) return 'Ongoing'
  return 'Completed'
}

useHead(() => ({
  title: series.value ? `${series.value.title} - Event Series - Ghost Guild` : 'Event Series - Ghost Guild',
  meta: [{ name: 'description', content: series.value?.description || 'Multi-event series' }],
}))
</script>

<style scoped>
.loading { padding: 48px 32px; color: var(--text-dim); }
.loading h2 { font-family: 'Brygada 1918', serif; font-size: 22px; color: var(--text-bright); margin-bottom: 8px; }

.back-link { padding: 12px 32px; border-bottom: 1px dashed var(--border); font-size: 12px; }
.back-link a { color: var(--candle); text-decoration: none; }

.series-header {
  padding: 28px 32px;
  border-bottom: 1px dashed var(--border);
}
.series-header h1 {
  font-family: 'Brygada 1918', serif;
  font-size: 28px;
  font-weight: 600;
  color: var(--text-bright);
  line-height: 1.15;
  margin-bottom: 12px;
}
.series-meta-row {
  display: flex;
  gap: 12px;
  align-items: center;
  font-size: 12px;
}
.meta-text { color: var(--text-faint); }

.section {
  padding: 24px 32px;
  border-bottom: 1px dashed var(--border);
}
.section p { font-size: 12px; color: var(--text-dim); line-height: 1.7; max-width: 560px; margin-bottom: 8px; }
.section a { font-size: 12px; color: var(--candle); }

.event-row {
  display: grid;
  grid-template-columns: 32px 80px 1fr;
  gap: 12px;
  align-items: baseline;
  padding: 10px 0;
  border-bottom: 1px dashed var(--border);
  font-size: 12px;
}
.event-row:last-child { border-bottom: none; }
.event-num { color: var(--text-faint); font-size: 11px; }
.event-date { color: var(--text-faint); }
.event-title-link { color: var(--text); text-decoration: none; font-size: 13px; }
.event-title-link:hover { color: var(--candle); }
.event-status { font-size: 10px; color: var(--text-faint); margin-left: 8px; }

.pass-price {
  font-family: 'Brygada 1918', serif;
  font-size: 22px;
  font-weight: 600;
  color: var(--candle);
  margin-top: 8px;
}

.empty { font-size: 12px; color: var(--text-faint); }
</style>
