import axiosInstance from "@/lib/axios";
import { useQuery } from "@tanstack/react-query";

// Fetcher that accepts targetedId as a number
const fetchSingleUser = async (targetedId: number) => {
  const userId = localStorage.getItem("userId");

  if (!userId) throw new Error("UserID not provided");

  const { data } = await axiosInstance.get(
    `/users/${userId}/dashboard/profile/${targetedId}`
  );

  return data;
};

// Hook that takes a number as targetedId
export const useGetSingleUser = (targetedId: number | any) => {
  return useQuery({
    queryKey: ["singleUser", targetedId],
    queryFn: () => fetchSingleUser(targetedId),
    enabled: !!targetedId, // ensures it only runs when ID is valid
    staleTime: 1000 * 60 * 10,
    refetchOnWindowFocus: false,
    refetchOnMount: "always",
  });
};
