// utils/hooks/useGetMatches.ts
import axiosInstance from "@/lib/axios";
import { useQuery } from "@tanstack/react-query";

// useGetMatches.ts
const fetchSubscribed = async (
  filters: Record<string, string>,
  page: number
) => {
  const userId = localStorage.getItem("userId");

  if (!userId) throw new Error("Missing user token or ID");

  const params = new URLSearchParams({
    page: page.toString(),
    per_page: "15",
  });

  // Append each filter only if it has a value
  Object.entries(filters).forEach(([key, value]) => {
    if (value && value.trim() !== "") {
      params.append(key, value);
    }
  });

  const { data } = await axiosInstance.get(
    `/users/${userId}/dashboard/subscription?${params.toString()}`
  );

  return data;
};

export const useGetSubscribed = (
  filters: Record<string, string>,
  page: number
) => {
  return useQuery({
    queryKey: ["subscribed", filters, page],
    queryFn: () => fetchSubscribed(filters, page),
    staleTime: 1000 * 60 * 10,
    refetchOnWindowFocus: false,
    refetchOnMount: "always",
  });
};
