import { useQuery } from '@tanstack/react-query';
import { useServices } from '@/lib/services/ServiceProvider';

export function useLeagueMemberships(leagueId: string) {
  const { leagueMembershipService } = useServices();

  return useQuery({
    queryKey: ['leagueMemberships', leagueId],
    queryFn: () => leagueMembershipService.fetchLeagueMemberships(leagueId),
    enabled: !!leagueId,
  });
}

export function useLeagueMembership(leagueId: string, driverId: string) {
  const { leagueMembershipService } = useServices();

  return useQuery({
    queryKey: ['leagueMembership', leagueId, driverId],
    queryFn: async () => {
      // First fetch memberships if not cached
      await leagueMembershipService.fetchLeagueMemberships(leagueId);
      return leagueMembershipService.getMembership(leagueId, driverId);
    },
    enabled: !!leagueId && !!driverId,
  });
}
