import { useMutation, useQueryClient } from "@tanstack/react-query";
import { apiPut } from "../../../../../lib/api/methods";
import { PaymentSettingsFormValues } from "./schema";
import { SETTINGS_ENDPOINTS } from "../endpoints";
import { errorToast, successToast } from "../../../../../utils/toast";
import { SUCCESS_MESSAGES } from "./messages";


export default function useUpdatePaymentSettings() {
  const queryClient = useQueryClient();
  const { mutateAsync, isPending } = useMutation({
    mutationKey: ["update-payment-settings"],
    mutationFn: async (data: PaymentSettingsFormValues) => {
      const res = await apiPut(SETTINGS_ENDPOINTS.PAYMENT, data);
      return res.data;
    },

    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["payment-settings"] });
      successToast(SUCCESS_MESSAGES.UPDATE_PAYMENT);
    },

    onError: () => {
      errorToast("Something Went Wrong");
    },
  });

  return {
    updatePaymentSettings: mutateAsync,
    updatingPaymentSettings: isPending,
  };
}
