import { useMutation } from "@tanstack/react-query";
import { AUTH_ENDPOINTS } from "../endpoints";
import { apiPost } from "../../../../lib/api/methods";
import { successToast, errorToast } from "../../../../utils/toast";
import { SUCCESS_MESSAGES } from "./messages";


type ResendEmailResponse = {
  message: string;
};

export default function useResendEmail() {
  const { mutateAsync, isPending } = useMutation({
    mutationKey: ["resend-email"],
    mutationFn: async (data: {email: string}) => {
      const res = await apiPost<ResendEmailResponse>(
        AUTH_ENDPOINTS.RESEND_EMAIL,
        data,
      );
      return res;
    },
    onSuccess: (data) => {
      successToast(SUCCESS_MESSAGES.RESEND_EMAIL);
    },
    onError: (error: any) => {
      errorToast(error?.response?.data?.message ?? "Something went wrong.");
    },
  });

  return { handleResendEmail: mutateAsync, loading: isPending };
}
