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

const fetchCompatibility = async (targetedId: number) => {
  const userId = localStorage.getItem("userId");
  const relationshipType = localStorage.getItem("relationshipType")

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

  const { data } = await axiosInstance.get(
    `/users/${userId}/dashboard/matches/${targetedId}?relationship_type=${relationshipType}` 
  );

  return data;
};

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