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


export default function usePlatformSettingsUpdate() {
  const queryClient = useQueryClient();
  const { mutateAsync, isPending } = useMutation({
    mutationKey: ["update-platform-settings"],
    mutationFn: async (data: PlatformSettingsFormValues) => {
      const response = await apiPut(SETTINGS_ENDPOINTS.PLATFORM, {
        ...data,
        dialCode: data?.mobileNumber?.dialCode,
        phone: data.mobileNumber.number,
      });
      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["platform-settings"] });
      successToast(SUCCESS_MESSAGES.UPDATE_PLATFORM);
    },
    onError: () => {
      errorToast("Something Went Wrong");
    },
  });

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