import { getCurrentTournament, getFreeAgents, getTournament, getUnenrolledTeams, listTournaments } from "./server";
import { useServerSuspenseQuery } from "@/lib/tanstack-query/hooks";

export const tournamentKeys = {
  list: ['tournaments', 'list'] as const,
  details: (id: string) => ['tournaments', 'details', id] as const,
  current: ['tournaments', 'current'] as const,
  unenrolled: (id: string) => ['tournaments', 'unenrolled', id] as const,
  free_agents: (id: string) => ['tournaments', 'free_agents', id] as const
};

export const tournamentQueries = {
  list: () => ({
    queryKey: tournamentKeys.list,
    queryFn: listTournaments
  }),
  details: (id: string) => ({
    queryKey: tournamentKeys.details(id),
    queryFn: () => getTournament({ data: id })
  }),
  current: () => ({
    queryKey: tournamentKeys.current,
    queryFn: getCurrentTournament
  }),
  unenrolled: (id: string) => ({
    queryKey: tournamentKeys.unenrolled(id),
    queryFn: () => getUnenrolledTeams({ data: id })
  }),
  free_agents: (id: string) => ({
    queryKey: tournamentKeys.free_agents(id),
    queryFn: () => getFreeAgents({ data: id })
  })
};

export const useTournaments = () => 
  useServerSuspenseQuery(tournamentQueries.list());

export const useTournament = (id: string) =>
  useServerSuspenseQuery(tournamentQueries.details(id));

export const useCurrentTournament = () =>
  useServerSuspenseQuery(tournamentQueries.current());

export const useUnenrolledTeams = (tournamentId: string) => 
  useServerSuspenseQuery(tournamentQueries.unenrolled(tournamentId));

export const useFreeAgents = (tournamentId: string) => 
  useServerSuspenseQuery(tournamentQueries.free_agents(tournamentId));
