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


export default function useUpdateSocialMediaSettings() {
  const queryClient = useQueryClient();
  const { mutateAsync, isPending } = useMutation({
    mutationKey: ["update-social-media-settings"],
    mutationFn: async (data: SocialMediaSettingsFormValues) => {
      const response = await apiPut(SETTINGS_ENDPOINTS.SOCIAL, data);
      return response.data;
    },
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["social-media-settings"] });
      queryClient.invalidateQueries({ queryKey: ["social-links"] });
      successToast(SUCCESS_MESSAGES.UPDATE_SOCIAL);
    },
    onError: () => {
      errorToast("Something Went Wrong");
    },
  });

  return {
    updateSocialMediaSettings: mutateAsync,
    updatingSocialMediaSettings: isPending,
  };
}
