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

export const useGetPrefferedMatchData = () => {
  const [enabled, setEnabled] = useState(false);

  useEffect(() => {
    // Enable query only when localStorage is available (in browser)
    if (typeof window !== "undefined") {
      setEnabled(true);
    }
  }, []);

  const fetchPrefferedMatchData = async () => {
    const userId = localStorage.getItem("userId");

    if (!userId) throw new Error("Token or user ID not found");

    const { data } = await axiosInstance.get(
      `/users/${userId}/preferred-matches`
    );

    return data;
  };

  return useQuery({
    queryKey: ["PrefferedMatchData"],
    queryFn: fetchPrefferedMatchData,
    enabled, // only runs after client-side mount
    staleTime: 1000 * 60 * 10,
    refetchOnWindowFocus: false,
  });
};
