import { useQuery, useMutation } from "@tanstack/react-query";
import { matchesService } from "./service";
import type { MatchListParams, MatchQueryDto } from "./types";

export const MatchesQueryKeys = {
  all: ["matches"] as const,
  list: (params?: MatchListParams) =>
    [...MatchesQueryKeys.all, "list", params] as const,
  detail: (id: string) => [...MatchesQueryKeys.all, "detail", id] as const,
  oddsMovement: (id: string) =>
    [...MatchesQueryKeys.all, "oddsMovement", id] as const,
  activeLeagues: (sport?: string) =>
    [...MatchesQueryKeys.all, "activeLeagues", sport] as const,
};

export const useListMatches = (params?: MatchListParams) => {
  return useQuery({
    queryKey: MatchesQueryKeys.list(params),
    queryFn: () => matchesService.listMatches(params),
  });
};

export const useMatchDetails = (id: string) => {
  return useQuery({
    queryKey: MatchesQueryKeys.detail(id),
    queryFn: () => matchesService.getMatchDetails(id),
    enabled: !!id,
  });
};

export const useOddsMovement = (id: string) => {
  return useQuery({
    queryKey: MatchesQueryKeys.oddsMovement(id),
    queryFn: () => matchesService.getOddsMovement(id),
    enabled: !!id,
  });
};

export const useQueryMatches = () => {
  return useMutation({
    mutationFn: (queryDto: MatchQueryDto) =>
      matchesService.queryMatches(queryDto),
  });
};

export const useActiveLeagues = (sport?: string) => {
  return useQuery({
    queryKey: MatchesQueryKeys.activeLeagues(sport),
    queryFn: () => matchesService.getActiveLeagues(sport),
  });
};
