import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { useServices } from '@/lib/services/ServiceProvider';
import { RaceResultsDetailViewModel } from '@/lib/view-models/RaceResultsDetailViewModel';

export function useRacesPageData() {
  const { raceService } = useServices();

  return useQuery({
    queryKey: ['racesPageData'],
    queryFn: () => raceService.getRacesPageData(),
  });
}

export function useAllRacesPageData() {
  const { raceService } = useServices();

  return useQuery({
    queryKey: ['allRacesPageData'],
    queryFn: () => raceService.getAllRacesPageData(),
  });
}

export function useRaceDetail(raceId: string, driverId: string) {
  const { raceService } = useServices();

  return useQuery({
    queryKey: ['raceDetail', raceId, driverId],
    queryFn: () => raceService.getRaceDetails(raceId, driverId),
    enabled: !!raceId && !!driverId,
  });
}

export function useRaceDetailMutation() {
  const { raceService } = useServices();
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ raceId, driverId }: { raceId: string; driverId: string }) =>
      raceService.getRaceDetails(raceId, driverId),
    onSuccess: (data, variables) => {
      queryClient.setQueryData(['raceDetail', variables.raceId, variables.driverId], data);
    },
  });
}

export function useRaceStewardingData(raceId: string, driverId: string) {
  const { raceStewardingService } = useServices();

  return useQuery({
    queryKey: ['raceStewardingData', raceId, driverId],
    queryFn: () => raceStewardingService.getRaceStewardingData(raceId, driverId),
    enabled: !!raceId && !!driverId,
  });
}

export function useRaceResultsDetail(raceId: string, driverId: string) {
  const { raceResultsService } = useServices();

  return useQuery<RaceResultsDetailViewModel>({
    queryKey: ['raceResultsDetail', raceId, driverId],
    queryFn: () => raceResultsService.getResultsDetail(raceId, driverId),
    enabled: !!raceId && !!driverId,
  });
}

export function useRaceWithSOF(raceId: string) {
  const { raceResultsService } = useServices();

  return useQuery({
    queryKey: ['raceWithSOF', raceId],
    queryFn: () => raceResultsService.getWithSOF(raceId),
    enabled: !!raceId,
  });
}

export function useRacesTotal() {
  const { raceService } = useServices();

  return useQuery({
    queryKey: ['racesTotal'],
    queryFn: () => raceService.getRacesTotal(),
  });
}

export function useRegisterForRace() {
  const { raceService } = useServices();
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ raceId, leagueId, driverId }: {
      raceId: string;
      leagueId: string;
      driverId: string;
    }) => raceService.registerForRace(raceId, leagueId, driverId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['racesPageData'] });
      queryClient.invalidateQueries({ queryKey: ['allRacesPageData'] });
    },
  });
}

export function useWithdrawFromRace() {
  const { raceService } = useServices();
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ raceId, driverId }: {
      raceId: string;
      driverId: string;
    }) => raceService.withdrawFromRace(raceId, driverId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['racesPageData'] });
      queryClient.invalidateQueries({ queryKey: ['allRacesPageData'] });
    },
  });
}

export function useCancelRace() {
  const { raceService } = useServices();
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (raceId: string) => raceService.cancelRace(raceId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['racesPageData'] });
      queryClient.invalidateQueries({ queryKey: ['allRacesPageData'] });
    },
  });
}

export function useCompleteRace() {
  const { raceService } = useServices();
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (raceId: string) => raceService.completeRace(raceId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['racesPageData'] });
      queryClient.invalidateQueries({ queryKey: ['allRacesPageData'] });
    },
  });
}

export function useReopenRace() {
  const { raceService } = useServices();
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (raceId: string) => raceService.reopenRace(raceId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['racesPageData'] });
      queryClient.invalidateQueries({ queryKey: ['allRacesPageData'] });
    },
  });
}
