import { useQuery, useMutation } from "@tanstack/react-query";
import {
	fetchDirectoryListing,
	fetchFfprobe,
	fetchStat,
	fetchJobTemplates,
	runJob,
} from "../api/client";

export function useDirectoryListing(path: string, machineId?: string) {
	return useQuery({
		queryKey: ["files", "list", path, machineId ?? "default"],
		queryFn: () => fetchDirectoryListing(path, machineId),
		enabled: !!path,
		staleTime: 30_000,
	});
}

export function useFfprobe(path: string, enabled = false, machineId?: string) {
	return useQuery({
		queryKey: ["files", "ffprobe", path, machineId ?? "default"],
		queryFn: () => fetchFfprobe(path, machineId),
		enabled: enabled && !!path,
		staleTime: 5 * 60_000,
	});
}

export function useStat(path: string, enabled = false, machineId?: string) {
	return useQuery({
		queryKey: ["files", "stat", path, machineId ?? "default"],
		queryFn: () => fetchStat(path, machineId),
		enabled: enabled && !!path,
	});
}

export function useJobTemplates() {
	return useQuery({
		queryKey: ["jobs", "templates"],
		queryFn: fetchJobTemplates,
		staleTime: 60_000,
	});
}

export function useRunJob(machineId?: string) {
	return useMutation({
		mutationFn: ({ jobKey, path }: { jobKey: string; path: string }) =>
			runJob(jobKey, path, machineId),
	});
}
