import * as api from "../service/SettingService";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { FaqPageEnum, SiteSettingPageEnum } from "../enum/Enum";
import { type CreateFaqType, type CreatePricingItem, type UpdateBannerType } from "../types/Types";

export const useGetAboutUs = () => {
  return useQuery({
    queryKey: ["aboutUs"],
    queryFn: api.getAboutUs,
  });
};

export const useCreateAboutUs = () => {
  return useMutation({
    mutationFn: api.createAboutUs,
  });
};

export const useDeleteAboutUs = () => {
  return useMutation({
    mutationFn: api.deleteAboutUs,
  });
};

export const useGetFaq = (page: FaqPageEnum) => {
  return useQuery({
    queryKey: ["faq", page],
    queryFn: () => api.getFaq(page),
  });
};

export const useCreateFaq = () => {
  return useMutation({
    mutationFn: api.createFaq,
  });
};

export const useDeleteFaq = () => {
  return useMutation({
    mutationFn: api.deleteFaq,
  });
};

export const useGetFaqDetail = (id: string) => {
  return useQuery({
    queryKey: ["faqDetail", id],
    queryFn: () => api.getFaqDetail(id),
    enabled: !!id,
  });
};

export const useUpdateFaq = () => {
  const queryClient = useQueryClient();
    return useMutation({
    mutationFn: ({id, params}: {id: string, params: CreateFaqType}) => api.updateFaq(id, params),
    onSuccess: (_, {params}) => {
      queryClient.invalidateQueries({ queryKey: ["faq", params.page] });
    },
  });
};

export const useGetSiteSetting = (page?: SiteSettingPageEnum) => {
  return useQuery({
    queryKey: page ? ["siteSetting", page] : ["siteSetting"],
    queryFn: () => api.getSiteSetting(page),
    enabled: true,
  });
};

export const useUpdateSiteSetting = () => {
  return useMutation({
    mutationFn: api.updateSiteSetting,
  });
};

export const useCreateDocument = () => {
  return useMutation({
    mutationFn: api.createDocument,
  });
};

export const useGetDocuments = () => {
  return useQuery({
    queryKey: ["documents"],
    queryFn: api.getDocuments,
  });
};

export const useDeleteDocument = () => {
  return useMutation({
    mutationFn: api.deleteDocument,
  });
};

export const useGetPricing = () => {
  return useQuery({
    queryKey: ["pricing"],
    queryFn: api.getPricing,
  });
};

export const useCreatePricing = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: api.createPricing,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["pricing"] });
    },
  });
};

export const useUpdatePricing = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({id, params}: {id: string, params: CreatePricingItem}) => api.updatePricing(id, params),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["pricing"] });
    },
  });
};

export const useDeletePricing = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: api.deletePricing,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["pricing"] });
    },
  });
};

export const useGetShop = () => {
  return useQuery({
    queryKey: ["shop"],
    queryFn: api.getShop,
  });
};

export const useUpdateShop = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: api.updateShop,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["shop"] });
    },
  });
};

export const useGetBanners = () => {
  return useQuery({
    queryKey: ["banners"],
    queryFn: api.getBanners,
  });
};

export const useDeleteBanner = () => {
  return useMutation({
    mutationFn: api.deleteBanner,
  });
};

export const useUpdateBanner = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({id, params}: {id: string, params: UpdateBannerType}) => api.updateBanner(id, params),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["banners"] });
    },
  });
};

export const useCreateBanner = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: api.createBanner,
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["banners"] });
    },
  });
};

