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

const useEnrollTeam = () => {
  const queryClient = useQueryClient();
  const { t } = useLingui();

  return useServerMutation({
    mutationFn: (data: { tournamentId: string, teamId: string }) => {
      return enrollTeam({ data });
    },
    onSuccess: (data, { tournamentId }) => {
      queryClient.invalidateQueries({ queryKey: ['tournaments', 'details', tournamentId] });
      queryClient.invalidateQueries({ queryKey: ['tournaments', 'unenrolled', tournamentId] });
    },
    successMessage: t`Team enrolled successfully!`,
  });
};

export default useEnrollTeam;
