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


export default function useUpdateSightEngineSettings() {
  const queryClient = useQueryClient();
  const { mutateAsync, isPending } = useMutation({
    mutationKey: ["update-sight-engine-settings"],
    mutationFn: async (data: SightEngineSettingsFormValues) => {
      const response = await apiPut(SETTINGS_ENDPOINTS.SIGHT_ENGINE, data);
      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["sight-engine-settings"] });
      successToast(SUCCESS_MESSAGES.UPDATE_SIGHT_ENGINE);
    },
    onError: () => {
      errorToast("Something Went Wrong");
    },
  });

  return {
    updateSightEngineSettings: mutateAsync,
    updatingSightEngineSettings: isPending,
  };
}
