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


type SendEmailToExistingResponse = {
  message: string;
};
export default function useSendEmailToExisting() {
  const { mutateAsync, isPending } = useMutation({
    mutationKey: ["send-email-to-existing"],
    mutationFn: async () => {
      const res = await apiPost<SendEmailToExistingResponse>(
        PROFILE_ENDPOINTS.SEND_CURRENT_EMAIL_OTP,
        {},
      );
      return res;
    },
    onSuccess: (data) => {
      successToast(SUCCESS_MESSAGES.SEND_OTP_EXISTING);
    },
    onError: (error: any) => {
      errorToast(error?.response?.data?.message ?? "Something went wrong.");
    },
  });

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