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

export const useContactUsMessages = (filters?: ContactUsFilters) => {
  return useQuery({
    queryKey: [QUERY_KEYS.GET_CONTACT_US_MESSAGES, filters],
    queryFn: () => getContactUsMessages(filters),
  });
};

export const useDeleteContactUsMessage = () => {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (id: string | number) => deleteContactUsMessage(id),
    onSuccess: () => {
      queryClient.invalidateQueries({
        queryKey: [QUERY_KEYS.GET_CONTACT_US_MESSAGES],
      });
      toast.success("پیام تماس با ما حذف شد");
    },
    onError: (error: any) => {
      toast.error(error?.message || "خطا در حذف پیام تماس با ما");
    },
  });
};
