import { useQuery, useMutation } from "@tanstack/react-query";
import { sporTotoService } from "./service";
import type {
  GenerateColumnsDto,
  EvaluateColumnsDto,
  GenerateSporTotoPredictionDto,
} from "./service";

export const SporTotoQueryKeys = {
  all: ["spor-toto"] as const,
  bulletins: (params?: { status?: string; limit?: number }) =>
    [...SporTotoQueryKeys.all, "bulletins", params] as const,
  bulletin: (id: string) => [...SporTotoQueryKeys.all, "bulletin", id] as const,
  bulletinStats: (id: string) =>
    [...SporTotoQueryKeys.all, "bulletin-stats", id] as const,
  rolloverHistory: (limit?: number) =>
    [...SporTotoQueryKeys.all, "rollover-history", limit] as const,
};

export const useBulletins = (status?: string, limit?: number) => {
  return useQuery({
    queryKey: SporTotoQueryKeys.bulletins({ status, limit }),
    queryFn: () => sporTotoService.listBulletins(status, limit),
  });
};

export const useBulletinById = (id: string) => {
  return useQuery({
    queryKey: SporTotoQueryKeys.bulletin(id),
    queryFn: () => sporTotoService.getBulletinById(id),
    enabled: !!id,
  });
};

export const useBulletinStats = (id: string) => {
  return useQuery({
    queryKey: SporTotoQueryKeys.bulletinStats(id),
    queryFn: () => sporTotoService.getBulletinStats(id),
    enabled: !!id,
  });
};

export const useRolloverHistory = (limit?: number) => {
  return useQuery({
    queryKey: SporTotoQueryKeys.rolloverHistory(limit),
    queryFn: () => sporTotoService.getRolloverHistory(limit),
  });
};

export const useSyncBulletin = () => {
  return useMutation({
    mutationFn: () => sporTotoService.syncFromApi(),
  });
};

export const useGenerateColumns = () => {
  return useMutation({
    mutationFn: (dto: GenerateColumnsDto) =>
      sporTotoService.generateColumns(dto),
  });
};

export const useEvaluateColumns = () => {
  return useMutation({
    mutationFn: (dto: EvaluateColumnsDto) =>
      sporTotoService.evaluateColumns(dto),
  });
};

export const useGeneratePrediction = () => {
  return useMutation({
    mutationFn: (dto: GenerateSporTotoPredictionDto) =>
      sporTotoService.generatePrediction(dto),
  });
};
