import { useMutation, useQueryClient } from "@tanstack/react-query";
import { apiDelete } from "../../../../lib/api/methods";
import { ADMIN_PACKAGES } from "./endpoints";
import { useParams } from "next/navigation";
import { errorToast, successToast } from "../../../../utils/toast";
import { SUCCESS_MESSAGES } from "./messages";


type DeletePackageResponse = {
  message: string;
};

export default function usePackageDelete() {
  const queryClient = useQueryClient();
  const { mutateAsync, isPending } = useMutation({
    mutationKey: ["package-delete"],
    mutationFn: async (id: string) => {
      const res = await apiDelete<DeletePackageResponse>(
        ADMIN_PACKAGES.DELETE_PACKAGE(id),
      );
      return res.data;
    },
    onSuccess: (data) => {
      queryClient.invalidateQueries({ queryKey: ["admin-packages"] });
      queryClient.invalidateQueries({ queryKey: ["packages"] });
      successToast(SUCCESS_MESSAGES.DELETE);
    },
    onError: (error: any) => {
      errorToast(error?.response?.data?.message ?? "Something went wrong.");
    },
  });

  return { handlePackageDelete: mutateAsync, loading: isPending };
}
