import { useQuery } from "@tanstack/react-query";
import {
	fetchAlertmanagerAlerts,
	fetchAlertmanagerStatus,
	fetchPrometheusStatus,
	fetchPrometheusTargets,
	fetchMonitoringMachines,
} from "../api/client";

export function useAlertmanagerAlerts(serviceId?: string) {
	return useQuery({
		queryKey: ["observability", "alerts", serviceId ?? ""],
		queryFn: () => fetchAlertmanagerAlerts(serviceId),
		retry: 2,
		staleTime: 10_000,
		refetchInterval: 15_000,
	});
}

export function useAlertmanagerStatus(serviceId?: string) {
	return useQuery({
		queryKey: ["observability", "alertmanager-status", serviceId ?? ""],
		queryFn: () => fetchAlertmanagerStatus(serviceId),
		retry: 2,
		staleTime: 10_000,
		refetchInterval: 30_000,
	});
}

export function usePrometheusStatus(serviceId?: string) {
	return useQuery({
		queryKey: ["observability", "prometheus-status", serviceId ?? ""],
		queryFn: () => fetchPrometheusStatus(serviceId),
		retry: 2,
		staleTime: 10_000,
		refetchInterval: 30_000,
	});
}

export function usePrometheusTargets() {
	return useQuery({
		queryKey: ["observability", "prometheus-targets"],
		queryFn: fetchPrometheusTargets,
		retry: 2,
		staleTime: 10_000,
		refetchInterval: 30_000,
	});
}

export function useMonitoringMachines() {
	return useQuery({
		queryKey: ["monitoring", "machines"],
		queryFn: fetchMonitoringMachines,
		retry: 2,
		staleTime: 10_000,
		refetchInterval: 30_000,
	});
}
