<template>
  <div class="min-h-full py-16 bg-gray-50">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
      <h1 class="text-4xl sm:text-5xl font-display font-bold text-gray-900 mb-6">
        Unsere Mannschaften
      </h1>
      <div class="w-24 h-1 bg-primary-600 mb-8" />
      
      <p class="text-xl text-gray-600 mb-12 flex flex-wrap items-center gap-2">
        <span>Unsere aktiven Mannschaften in der Saison</span>
        <label
          for="season-select"
          class="sr-only"
        >
          Saison auswählen
        </label>
        <select
          id="season-select"
          v-model="selectedSeason"
          class="px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-primary-500 focus:border-primary-500 bg-white text-base"
        >
          <option
            v-for="season in seasons"
            :key="season"
            :value="season"
          >
            {{ formatSeasonLabel(season) }}
          </option>
        </select>
      </p>

      <MannschaftenUebersicht :season="selectedSeason" />

      <div class="mt-16">
        <div class="bg-primary-50 p-8 rounded-xl border border-primary-100">
          <h3 class="text-2xl font-display font-bold text-gray-900 mb-4">
            Spielpläne & Ergebnisse
          </h3>
          <p class="text-gray-600 mb-6">
            Alle aktuellen Spielpläne und Ergebnisse unserer Mannschaften finden Sie hier.
          </p>
          <NuxtLink
            :to="{ path: '/mannschaften/spielplaene', query: { season: selectedSeason } }"
            class="inline-flex items-center px-6 py-3 bg-primary-600 hover:bg-primary-700 text-white font-semibold rounded-lg transition-colors"
          >
            Zu den Spielplänen
          </NuxtLink>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { computed } from 'vue'
import MannschaftenUebersicht from '~/components/MannschaftenUebersicht.vue'

const route = useRoute()
const router = useRouter()

const getCurrentSeasonSlug = () => {
  const now = new Date()
  const year = now.getFullYear()
  const startYear = now.getMonth() >= 6 ? year : year - 1
  const endYear = startYear + 1
  return `${String(startYear).slice(-2)}--${String(endYear).slice(-2)}`
}

const currentSeason = getCurrentSeasonSlug()
const { data: seasonsResult } = await useFetch('/api/mannschaften/seasons')

const seasons = computed(() => {
  const availableSeasons = Array.isArray(seasonsResult.value?.seasons)
    ? seasonsResult.value.seasons
    : []

  return [...new Set([currentSeason, ...availableSeasons])]
    .filter(season => /^\d{2}--\d{2}$/.test(season))
    .sort()
    .reverse()
})

const selectedSeason = computed({
  get() {
    const value = String(route.query.season || '').trim()
    return seasons.value.includes(value) ? value : currentSeason
  },
  set(value) {
    const season = seasons.value.includes(value) ? value : currentSeason
    router.replace({ query: { ...route.query, season } })
  }
})

const formatSeasonLabel = (season) => {
  const match = String(season || '').match(/^(\d{2})--(\d{2})$/)
  return match ? `20${match[1]}/${match[2]}` : season
}

useHead({
  title: 'Mannschaften - Harheimer TC',
})
</script>
