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


type ForgotPasswordResponse = {
  message: string;
};

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

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