import { API_ENDPOINTS, inquiriesService } from "@/lib/api";
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { useMemo } from "react";
import { toast } from "react-toastify";

export const useGetInquiries = (params?: Record<string, any>) => {
  const queryKey = useMemo(
    () => [API_ENDPOINTS.INQUIRIES.BASE(), params],
    [params],
  );
  return useQuery({
    queryKey,
    queryFn: () => inquiriesService.getInquiries(params),
  });
};
export const useDeleteInquiry = (successCallback?: () => void) => {
  const queryClient = useQueryClient();
  const mutationKey = useMemo(
    () => [API_ENDPOINTS.INQUIRIES.BASE(), "DELETE INQUIRY"],
    [],
  );
  return useMutation({
    mutationKey,
    mutationFn: inquiriesService.deleteInquiry,
    onSuccess: (response) => {
      toast.success(response.message);
      successCallback?.();
      queryClient.invalidateQueries({
        queryKey: [API_ENDPOINTS.INQUIRIES.BASE()],
      });
    },
  });
};
export const useChangeInquiryStatus = (successCallback?: () => void) => {
  const queryClient = useQueryClient();
  const mutationKey = useMemo(
    () => [API_ENDPOINTS.INQUIRIES.BASE(), "UPDATE INQUIRY STATUS"],
    [],
  );
  return useMutation({
    mutationKey,
    mutationFn: inquiriesService.changeInquiryStatus,
    onSuccess: (response) => {
      toast.success(response.message);
      successCallback?.()
      queryClient.invalidateQueries({
        queryKey: [API_ENDPOINTS.INQUIRIES.BASE()],
      });
    },
  });
};
