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


type SendNewEmailOtpResponse = {
  message: string;
};

export default function useSendNewEmailOtp() {
  const { mutateAsync, isPending } = useMutation({
    mutationKey: ["send-new-email-otp"],
    mutationFn: async (data: { email: string }) => {
      const res = await apiPost<SendNewEmailOtpResponse>(
        PROFILE_ENDPOINTS.SEND_NEW_EMAIL_OTP,
        {newEmail: data.email},
      );
      return res;
    },
    onSuccess: (data) => {
      successToast(SUCCESS_MESSAGES.SEND_OTP_NEW);
    },
    onError: (error: any) => {
      errorToast(error?.response?.data?.message ?? "Something went wrong.");
    },
  });

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