import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
import {
	fetchMediaStatus,
	buildMediaIndex,
	queryMedia,
	stopMediaIndexBuild,
	forceStopMediaIndexBuild,
} from "../api/client";

export function useMediaStatus(jellyfinServiceId?: string) {
	return useQuery({
		queryKey: ["media", "status", jellyfinServiceId ?? "default"],
		queryFn: () => fetchMediaStatus(jellyfinServiceId),
		staleTime: 5_000,
		refetchInterval: (query) =>
			query.state.data?.build_running ? 1000 : false,
		// Inherit the default refetchIntervalInBackground: false — pause the
		// 1s build-progress poll when the tab is hidden. The build keeps
		// running server-side; the poll resumes and catches up on return.
		// Battery-friendly (D8 follow-up).
	});
}

export function useMediaQuery(params: {
	libraries?: string;
	types?: string;
	search?: string;
	hdr_filter?: string;
	sort_key?: string;
	sort_order?: string;
	limit?: number;
	offset?: number;
	jellyfinServiceId?: string;
	enabled?: boolean;
}) {
	const { enabled = true, ...queryParams } = params;

	return useQuery({
		queryKey: ["media", "query", queryParams],
		queryFn: () => queryMedia(queryParams),
		enabled,
		staleTime: 30_000,
	});
}

function invalidateMedia(queryClient: ReturnType<typeof useQueryClient>) {
	queryClient.invalidateQueries({ queryKey: ["media"] });
}

export function useBuildIndex(jellyfinServiceId?: string) {
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: () => buildMediaIndex(jellyfinServiceId),
		onSuccess: () => {
			invalidateMedia(queryClient);
		},
		onError: () => {
			// Invalidate status so the UI reflects the current (non-building) state.
			invalidateMedia(queryClient);
		},
	});
}

export function useStopBuildIndex(jellyfinServiceId?: string) {
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: () => stopMediaIndexBuild(jellyfinServiceId),
		onSuccess: () => {
			invalidateMedia(queryClient);
		},
	});
}

export function useForceStopBuildIndex(jellyfinServiceId?: string) {
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: () => forceStopMediaIndexBuild(jellyfinServiceId),
		onSuccess: () => {
			invalidateMedia(queryClient);
		},
	});
}
