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 { SUCCESS_MESSAGES } from "./messages";


export type SecuritySettingsUpdatePayload = {
  privacyPolicy?: string;
  termsAndConditions?: string;
};

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

  return {
    updateSecuritySettings: mutateAsync,
    updatingSecuritySettings: isPending,
  };
}
