import { create } from 'zustand';
import { persist } from 'zustand/middleware';

export type ContestSummary = {
  id: number;
  name: string;              // už přeložené jméno podle locale
  description?: string | null;
  is_active: boolean;
  is_test: boolean;
  is_mcr: boolean;
  is_sixhr: boolean;
  start_time: string | null; // ISO string z API
  duration: number;          // hodiny
  logs_deadline_days: number;
  rounds?: RoundSummary[];
};

export type RoundSummary = {
  id: number;
  contest_id: number;
  name: string;
  description?: string | null;
  bands?: { id: number; name: string }[];
  categories?: { id: number; name: string }[];
  power_categories?: { id: number; name: string }[];
  is_active: boolean;
  is_test: boolean;
  is_sixhr: boolean;
  start_time: string | null;
  end_time: string | null;
  logs_deadline: string | null;
  preliminary_evaluation_run_id?: number | null;
  official_evaluation_run_id?: number | null;
  test_evaluation_run_id?: number | null;
};

type ContestState = {
  selectedContest: ContestSummary | null;
  selectedRound: RoundSummary | null;
  selectedContestRounds: RoundSummary[];
  setSelectedContest: (contest: ContestSummary | null) => void;
  setSelectedRound: (round: RoundSummary | null) => void;
  setSelectedContestRounds: (rounds: RoundSummary[]) => void;
  clearSelection: () => void;
};

export const useContestStore = create<ContestState>()(
  persist(
    (set) => ({
      selectedContest: null,
      selectedRound: null,
      selectedContestRounds: [],
      setSelectedContest: (contest) =>
        set({
          selectedContest: contest,
          // při přepnutí závodu implicitně zruš vybrané kolo
          selectedRound: null,
          selectedContestRounds: contest?.rounds ?? [],
        }),
      setSelectedRound: (round) => set({ selectedRound: round }),
      setSelectedContestRounds: (rounds) => set({ selectedContestRounds: rounds }),
      clearSelection: () =>
        set({
          selectedContest: null,
          selectedRound: null,
          selectedContestRounds: [],
        }),
    }),
    {
      name: 'contest-store',
      partialize: (state) => ({
        selectedContest: state.selectedContest,
        selectedRound: state.selectedRound,
        selectedContestRounds: state.selectedContestRounds,
      }),
    }
  )
);

// Použití:
// import { useContestStore } from '@/stores/contestStore';
// const selectedContest = useContestStore((s) => s.selectedContest);
// const selectedRound = useContestStore((s) => s.selectedRound);
// const setSelectedContest = useContestStore((s) => s.setSelectedContest);
// const setSelectedRound = useContestStore((s) => s.setSelectedRound);
// po kliknutí v seznamu závodů:
//setSelectedContest(contestFromApi);
