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


type ResetPasswordResponse = {
  message: string;
};

export default function useResetPassword() {

  const { mutateAsync, isPending } = useMutation({
    mutationKey: ["reset-password"],
    mutationFn: async (data: ResetPasswordSchema & { token: string }) => {
      const res = await apiPost<ResetPasswordResponse>(
        AUTH_ENDPOINTS.RESET_PASSWORD,
        data,
      );
      return res;
    },
    onSuccess: (data) => {
      successToast(SUCCESS_MESSAGES.RESET_PASSWORD);
    },
    onError: (error: any) => {
      errorToast(error?.response?.data?.message ?? "Something went wrong.");
    },
  });

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