import { apiClient } from "./client";

export interface ReadinessProbe {
	command: string;
	timeout: number;
	interval: number;
}

export interface ToolType {
	id: string;
	name: string;
	display_name: string;
	description: string | null;
	category: string;
	interface_type: string;
	requires_port: boolean;
	default_port: number | null;
	definition_type: "compose" | "dockerfile" | "manifest";
	manifest_id: string | null;
	compose_template: string | null;
	dockerfile_template: string | null;
	build_context: Record<string, string> | null;
	readiness_probe: ReadinessProbe | null;
	startup_command: string | null;
	required_variables: string[];
	created_by_id: string | null;
	created_at: string;
	updated_at: string;
}

export interface CreateToolTypeRequest {
	name: string;
	display_name: string;
	description?: string;
	category?: string;
	interface_type?: string;
	requires_port?: boolean;
	default_port: number;
	definition_type?: "compose" | "dockerfile" | "manifest";
	manifest_id?: string;
	compose_template?: string;
	dockerfile_template?: string;
	build_context?: Record<string, string>;
	readiness_probe?: ReadinessProbe;
	startup_command?: string;
	required_variables: string[];
}

export interface UpdateToolTypeRequest {
	display_name?: string;
	description?: string;
	category?: string;
	interface_type?: string;
	requires_port?: boolean;
	default_port?: number;
	definition_type?: "compose" | "dockerfile" | "manifest";
	manifest_id?: string;
	compose_template?: string;
	dockerfile_template?: string;
	build_context?: Record<string, string>;
	readiness_probe?: ReadinessProbe;
	startup_command?: string;
	required_variables?: string[];
}

export const listToolTypes = async (): Promise<ToolType[]> => {
	const response = await apiClient.get<ToolType[]>("/tool-types");
	return response.data;
};

export const getToolType = async (id: string): Promise<ToolType> => {
	const response = await apiClient.get<ToolType>(`/tool-types/${id}`);
	return response.data;
};

export const createToolType = async (
	data: CreateToolTypeRequest,
): Promise<ToolType> => {
	const response = await apiClient.post<ToolType>("/tool-types", data);
	return response.data;
};

export const updateToolType = async (
	id: string,
	data: UpdateToolTypeRequest,
): Promise<ToolType> => {
	const response = await apiClient.put<ToolType>(`/tool-types/${id}`, data);
	return response.data;
};

export const deleteToolType = async (id: string): Promise<void> => {
	await apiClient.delete(`/tool-types/${id}`);
};

export const validateToolType = async (
	id: string,
): Promise<{ valid: boolean; errors?: string[] }> => {
	const response = await apiClient.get<{ valid: boolean; errors?: string[] }>(
		`/tool-types/${id}/validate`,
	);
	return response.data;
};
