'use client'

import { createContext, useContext, useState, useEffect, type ReactNode } from 'react'
import { trpc } from '@/lib/trpc/client'

type Program = {
  id: string
  name: string | null
  year?: number
  rounds?: Array<{ id: string; name: string; status: string; competitionId?: string }>
}

type EditionContextValue = {
  programs: Program[]
  selectedProgramId: string
  setSelectedProgramId: (id: string) => void
  activeRoundId: string
}

const EditionContext = createContext<EditionContextValue | null>(null)

export function useEditionContext() {
  const ctx = useContext(EditionContext)
  if (!ctx) throw new Error('useEditionContext must be used within EditionProvider')
  return ctx
}

function findBestRound(rounds: Array<{ id: string; status: string }>): string {
  const active = rounds.find(r => r.status === 'ROUND_ACTIVE')
  if (active) return active.id
  const closed = [...rounds].filter(r => r.status === 'ROUND_CLOSED').pop()
  if (closed) return closed.id
  return rounds[0]?.id ?? ''
}

export function EditionProvider({ children }: { children: ReactNode }) {
  const [selectedProgramId, setSelectedProgramId] = useState<string>('')

  const { data: programs } = trpc.program.list.useQuery(
    { includeStages: true },
    { refetchInterval: 30_000 },
  )

  useEffect(() => {
    if (programs && programs.length > 0 && !selectedProgramId) {
      setSelectedProgramId(programs[0].id)
    }
  }, [programs, selectedProgramId])

  const typedPrograms = (programs ?? []) as Program[]
  const selectedProgram = typedPrograms.find(p => p.id === selectedProgramId)
  const rounds = (selectedProgram?.rounds ?? []) as Array<{ id: string; status: string }>
  const activeRoundId = findBestRound(rounds)

  return (
    <EditionContext.Provider
      value={{
        programs: typedPrograms,
        selectedProgramId,
        setSelectedProgramId,
        activeRoundId,
      }}
    >
      {children}
    </EditionContext.Provider>
  )
}
