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


type UpdatePasswordResponse = {
  message: string;
};

export default function useUpdatePassword() {
  const { mutateAsync, isPending, error, isError } = useMutation({
    mutationKey: ["update-password"],
    mutationFn: async (data: UpdatePasswordSchema) => {
      const res = await apiPost<UpdatePasswordResponse>(
        AUTH_ENDPOINTS.UPDATE_PASSWORD,
        data,
      );
      return res;
    },
    onSuccess: (data) => {
      successToast(SUCCESS_MESSAGES.UPDATE_PASSWORD);
    },
    onError: (error: any) => {
      errorToast(error?.response?.data?.message ?? "Something went wrong.");
    },
  });

  return { handleUpdatePassword: mutateAsync, loading: isPending, isError };
}
