import { useMutation, type UseMutationOptions } from "@tanstack/react-query";
import instance from "@/instance/instance";
import { toast } from "sonner";

const useDeleteData = ({
  url,
  mutationOptions = {},
}: {
  url: string;

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  mutationOptions?: UseMutationOptions<any, Error, string>;
}) => {
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  return useMutation<any, Error, string>({
    mutationFn: async (id: string) => {
      const headers: Record<string, string> = {};

      const response = await instance.delete(`${url}${String(id)}`, {
        headers,
      });

      if (response?.statusText === "OK" || response?.status == 200) {
        toast.success(response?.data?.message || "Deleted successfully.");
        return response.data?.data;
      }

      throw new Error(response?.data?.message || "Failed to delete resource.");
    },
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    onError: (error: any) => {
      toast.error(
        error?.response?.data?.message || "An error occurred during deletion."
      );
      console.log("errorerrorerror", error);
    },

    ...mutationOptions,
    retry: false,
  });
};

export default useDeleteData;
