import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import {
	deleteMonitoringMachine,
	deleteSSHKey,
	fetchMonitoringSettings,
	fetchSSHKeys,
	fetchSavedTaskRuns,
	fetchSavedTasks,
	generateSSHKey,
	resetLocalDatabase,
	saveMonitoringMachine,
	saveSSHKey,
	saveTask,
	deleteTask,
	runTask,
	testMonitoringMachineSSH,
} from "../api/client";
import type {
	MonitoringMachineInput,
	ResetLocalDatabaseInput,
	SavedTaskInput,
	SSHKeyInput,
	SSHValidationResult,
} from "../types";

export function useMonitoringSettings() {
	return useQuery({
		queryKey: ["settings", "monitoring-machines"],
		queryFn: fetchMonitoringSettings,
		refetchInterval: 30_000,
	});
}

export function useSSHKeys() {
	return useQuery({
		queryKey: ["settings", "ssh-keys"],
		queryFn: fetchSSHKeys,
		refetchInterval: 30_000,
	});
}

export function useGenerateSSHKey() {
	return useMutation({
		mutationFn: (payload: {
			name: string;
			passphrase: string;
			notes: string;
			bits?: number;
		}) => generateSSHKey(payload),
	});
}

export function useSaveSSHKey() {
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: (key: SSHKeyInput) => saveSSHKey(key),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["settings"] });
		},
	});
}

export function useDeleteSSHKey() {
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: (keyId: string) => deleteSSHKey(keyId),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["settings"] });
			queryClient.invalidateQueries({ queryKey: ["monitoring"] });
		},
	});
}

export function useTasks() {
	return useQuery({
		queryKey: ["tasks"],
		queryFn: fetchSavedTasks,
		refetchInterval: 30_000,
	});
}

export function useTaskRuns(taskId?: string) {
	return useQuery({
		queryKey: ["tasks", taskId ?? "none", "runs"],
		queryFn: () => fetchSavedTaskRuns(taskId ?? ""),
		enabled: Boolean(taskId),
		refetchInterval: 30_000,
	});
}

export function useSaveTask() {
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: (task: SavedTaskInput) => saveTask(task),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["tasks"] });
		},
	});
}

export function useDeleteTask() {
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: (taskId: string) => deleteTask(taskId),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["tasks"] });
		},
	});
}

export function useRunTask() {
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: ({
			taskId,
			serviceId,
		}: {
			taskId: string;
			serviceId?: string;
		}) => runTask(taskId, serviceId),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["tasks"] });
		},
	});
}

export function useSaveMonitoringMachine() {
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: (machine: MonitoringMachineInput) =>
			saveMonitoringMachine(machine),
		onSettled: () => {
			queryClient.invalidateQueries({ queryKey: ["settings"] });
			queryClient.invalidateQueries({ queryKey: ["monitoring"] });
		},
	});
}

export function useTestMonitoringMachineSSH() {
	return useMutation<SSHValidationResult, Error, MonitoringMachineInput>({
		mutationFn: testMonitoringMachineSSH,
	});
}

export function useDeleteMonitoringMachine() {
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: (machineId: string) => deleteMonitoringMachine(machineId),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["settings"] });
			queryClient.invalidateQueries({ queryKey: ["monitoring"] });
		},
	});
}

export function useResetLocalDatabase() {
	const queryClient = useQueryClient();
	return useMutation({
		mutationFn: (payload: ResetLocalDatabaseInput) =>
			resetLocalDatabase(payload),
		onSuccess: () => {
			queryClient.invalidateQueries({ queryKey: ["settings"] });
			queryClient.invalidateQueries({ queryKey: ["monitoring"] });
			queryClient.invalidateQueries({ queryKey: ["media"] });
			queryClient.invalidateQueries({ queryKey: ["dashboard"] });
		},
	});
}
