import { create } from "zustand";
import type { SportType } from "@/lib/api/matches/types";

interface MatchState {
  selectedMatchIds: Set<string>;
  sport: SportType;
  leagueFilter: string | null;
}

interface MatchActions {
  toggleMatch: (matchId: string) => void;
  clearSelection: () => void;
  setSport: (sport: SportType) => void;
  setLeague: (leagueId: string | null) => void;
  isSelected: (matchId: string) => boolean;
}

type MatchStore = MatchState & MatchActions;

export const useMatchStore = create<MatchStore>()((set, get) => ({
  selectedMatchIds: new Set<string>(),
  sport: "football",
  leagueFilter: null,

  toggleMatch: (matchId) =>
    set((state) => {
      const next = new Set(state.selectedMatchIds);
      if (next.has(matchId)) {
        next.delete(matchId);
      } else {
        next.add(matchId);
      }
      return { selectedMatchIds: next };
    }),

  clearSelection: () => set({ selectedMatchIds: new Set<string>() }),

  setSport: (sport) => set({ sport, leagueFilter: null }),

  setLeague: (leagueId) => set({ leagueFilter: leagueId }),

  isSelected: (matchId) => get().selectedMatchIds.has(matchId),
}));
