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 VerifyCurrentEmailResponse = {
    message: string;
};

export default function useVerifyCurrentEmail() {
    const { mutateAsync, isPending } = useMutation({
        mutationKey: ["verify-current-email"],
        mutationFn: async (data: { otp: string }) => {
            const res = await apiPost<VerifyCurrentEmailResponse>(
                PROFILE_ENDPOINTS.EMAIL_VERIFY_EXISTING,
                data,
            );
            return res;
        },
        onSuccess: (data) => {
            successToast(SUCCESS_MESSAGES.VERIFY_EXISTING);
        },
        onError: (error: any) => {
            errorToast(error?.response?.data?.message ?? "Something went wrong.");
        },
    });

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