import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import { QUERY_KEYS } from "@/utils/query-key";
import { getLandingHero, updateLandingHero } from "./_requests";
import { LandingHeroData } from "./_models";
import toast from "react-hot-toast";

export const useLandingHero = () => {
  return useQuery({
    queryKey: [QUERY_KEYS.GET_LANDING_HERO],
    queryFn: getLandingHero,
  });
};

export const useUpdateLandingHero = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationKey: [QUERY_KEYS.UPDATE_LANDING_HERO],
    mutationFn: (data: LandingHeroData) => updateLandingHero(data),
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: [QUERY_KEYS.GET_LANDING_HERO],
      });
      toast.success("اسلایدر با موفقیت به‌روزرسانی شد");
    },
    onError: (error: any) => {
      console.error("Update landing hero error", error);
      toast.error(error?.message || "خطا در به‌روزرسانی اسلایدر");
    },
  });
};
