import { useQuery } from "@tanstack/react-query";
import {
	fetchJellyseerrRequests,
	fetchJellyseerrStats,
	type JellyseerRequest,
	type JellyseerStatsResponse,
} from "../api/jellyseerr";

/** Poll Jellyseerr request stats for a Jellyfin service (shares the backend cache). */
export function useJellyseerrStats(jellyfinServiceId?: string) {
	return useQuery<JellyseerStatsResponse, Error, JellyseerStatsResponse>({
		queryKey: ["jellyseerr", "stats", jellyfinServiceId ?? "default"],
		queryFn: () => fetchJellyseerrStats(jellyfinServiceId),
		refetchInterval: 60_000,
		// The backend surfaces "not configured"/unreachable as normal responses or
		// 503s; don't hammer it with retries.
		retry: false,
	});
}

/** Poll Jellyseerr requests for the Requests tab table. */
export function useJellyseerRequests(jellyfinServiceId?: string) {
	return useQuery<JellyseerRequest[], Error, JellyseerRequest[]>({
		queryKey: ["jellyseerr", "requests", jellyfinServiceId ?? "default"],
		queryFn: () => fetchJellyseerrRequests(jellyfinServiceId),
		refetchInterval: 60_000,
		retry: false,
	});
}
