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


export default function useUserStatusUpdate(message?: string) {
  const { id } = useParams<{ id: string }>();
  const queryClient = useQueryClient();
  const { mutateAsync, isPending } = useMutation({
    mutationKey: ["user-delete"],
    mutationFn: async (id: string) => {
      const res = await apiDelete(ADMIN_USERS.DELETE_USER(id));
      return res;
    },
    onSuccess: (data) => {
      queryClient.invalidateQueries({ queryKey: ["users"] });
      if (id) {
        queryClient.invalidateQueries({ queryKey: ["user-detail", id] });
      }
      successToast(SUCCESS_MESSAGES.DELETE_USER);
    },
    onError: (error: any) => {
      errorToast(error?.response?.data?.message ?? "Something went wrong.");
    },
  });

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