import { useQueryClient } from "@tanstack/react-query";
import { useLingui } from "@lingui/react/macro";
import { useServerMutation } from "@/lib/tanstack-query/hooks";
import { enrollFreeAgent } from "@/features/tournaments/server";
import { tournamentKeys } from "../queries";

const useEnrollFreeAgent = (isRegional?: boolean) => {
  const queryClient = useQueryClient();
  const { t } = useLingui();

  return useServerMutation({
    mutationFn: (data: { tournamentId: string, playerId: string, phone: string }) => {
      return enrollFreeAgent({ data });
    },
    onSuccess: (data, { tournamentId }) => {
      queryClient.invalidateQueries({ queryKey: tournamentKeys.free_agents(tournamentId) });
    },
    successMessage: isRegional ? t`You've enrolled in regionals!` : t`You've been added as a free agent!`,
  });
};

export default useEnrollFreeAgent;
