import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
  createGuide,
  deleteGuide,
  getGuideDetail,
  getGuides,
  updateGuide,
  UpdateGuideType,
} from "../service/GuuideService";
import { CreateGuideType } from "../types/Types";

export const useGetGuides = (id: string) => {
  return useQuery({
    queryKey: ["guides", id],
    queryFn: () => getGuides(id),
    enabled: !!id,
  });
};

export const useCreateGuide = () => {
  return useMutation({
    mutationFn: (variables: CreateGuideType) => createGuide(variables),
  });
};

export const useDeleteGuide = (serviceId: string) => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (guideId: string) => deleteGuide(guideId),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["guides", serviceId] });
    },
  });
};

export const useGetGuideDetail = (id: string) => {
  return useQuery({
    queryKey: ["guide-detail", id],
    queryFn: () => getGuideDetail(id),
    enabled: !!id,
  });
};

export const useUpdateGuide = (serviceId: string) => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, params }: { id: string; params: UpdateGuideType }) =>
      updateGuide(id, params),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ["guides", serviceId] });
    },
  });
};
