import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { useServices } from '@/lib/services/ServiceProvider';
import { CreateLeagueInputDTO } from '@/lib/types/generated/CreateLeagueInputDTO';
import { LeagueSummaryViewModel } from '@/lib/view-models/LeagueSummaryViewModel';
import { LeagueStandingsViewModel } from '@/lib/view-models/LeagueStandingsViewModel';
import { LeagueStatsViewModel } from '@/lib/view-models/LeagueStatsViewModel';
import { LeagueScheduleViewModel } from '@/lib/view-models/LeagueScheduleViewModel';
import { LeagueMembershipsViewModel } from '@/lib/view-models/LeagueMembershipsViewModel';
import { RemoveMemberViewModel } from '@/lib/view-models/RemoveMemberViewModel';
import { LeagueDetailViewModel } from '@/lib/view-models/LeagueDetailViewModel';
import { LeagueDetailPageViewModel } from '@/lib/view-models/LeagueDetailPageViewModel';

export function useAllLeagues() {
  const { leagueService } = useServices();

  return useQuery({
    queryKey: ['allLeagues'],
    queryFn: () => leagueService.getAllLeagues(),
  });
}

export function useLeagueStandings(leagueId: string, currentUserId: string) {
  const { leagueService } = useServices();

  return useQuery({
    queryKey: ['leagueStandings', leagueId, currentUserId],
    queryFn: () => leagueService.getLeagueStandings(leagueId, currentUserId),
    enabled: !!leagueId && !!currentUserId,
  });
}

export function useLeagueStats() {
  const { leagueService } = useServices();

  return useQuery({
    queryKey: ['leagueStats'],
    queryFn: () => leagueService.getLeagueStats(),
  });
}

export function useLeagueSchedule(leagueId: string) {
  const { leagueService } = useServices();

  return useQuery({
    queryKey: ['leagueSchedule', leagueId],
    queryFn: () => leagueService.getLeagueSchedule(leagueId),
    enabled: !!leagueId,
  });
}

export function useLeagueMemberships(leagueId: string, currentUserId: string) {
  const { leagueService } = useServices();

  return useQuery({
    queryKey: ['leagueMemberships', leagueId, currentUserId],
    queryFn: () => leagueService.getLeagueMemberships(leagueId, currentUserId),
    enabled: !!leagueId && !!currentUserId,
  });
}

export function useCreateLeague() {
  const { leagueService } = useServices();
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: (input: CreateLeagueInputDTO) => leagueService.createLeague(input),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['allLeagues'] });
    },
  });
}

export function useRemoveLeagueMember() {
  const { leagueService } = useServices();
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ leagueId, performerDriverId, targetDriverId }: {
      leagueId: string;
      performerDriverId: string;
      targetDriverId: string;
    }) => leagueService.removeMember(leagueId, performerDriverId, targetDriverId),
    onSuccess: (data, variables) => {
      queryClient.invalidateQueries({ queryKey: ['leagueMemberships', variables.leagueId] });
      queryClient.invalidateQueries({ queryKey: ['leagueStandings', variables.leagueId] });
    },
  });
}

export function useUpdateLeagueMemberRole() {
  const { leagueService } = useServices();
  const queryClient = useQueryClient();

  return useMutation({
    mutationFn: ({ leagueId, performerDriverId, targetDriverId, newRole }: {
      leagueId: string;
      performerDriverId: string;
      targetDriverId: string;
      newRole: string;
    }) => leagueService.updateMemberRole(leagueId, performerDriverId, targetDriverId, newRole),
    onSuccess: (data, variables) => {
      queryClient.invalidateQueries({ queryKey: ['leagueMemberships', variables.leagueId] });
    },
  });
}

export function useLeagueDetail(leagueId: string, currentDriverId: string) {
  const { leagueService } = useServices();

  return useQuery({
    queryKey: ['leagueDetail', leagueId, currentDriverId],
    queryFn: () => leagueService.getLeagueDetail(leagueId, currentDriverId),
    enabled: !!leagueId && !!currentDriverId,
  });
}

export function useLeagueDetailPageData(leagueId: string) {
  const { leagueService } = useServices();

  return useQuery({
    queryKey: ['leagueDetailPageData', leagueId],
    queryFn: () => leagueService.getLeagueDetailPageData(leagueId),
    enabled: !!leagueId,
  });
}