import { getCurrentSeasonSlug, listSpielplanSeasons } from '../../utils/spielplan-data.js'
import { error as loggerError } from '../../utils/logger.js'

export default defineEventHandler(async () => {
  try {
    const seasons = await listSpielplanSeasons()
    const currentSeason = getCurrentSeasonSlug()
    const defaultSeason = seasons.find((season) => season.slug === currentSeason)?.slug || seasons[0]?.slug || null

    return {
      success: true,
      seasons,
      currentSeason,
      defaultSeason
    }
  } catch (error) {
    loggerError('Fehler beim Laden der Spielplan-Saisons:', { error })
    return {
      success: false,
      seasons: [],
      currentSeason: getCurrentSeasonSlug(),
      defaultSeason: null
    }
  }
})
