/**
 * Tool wrapper - wraps tools with hook callbacks for interception.
 */

import type { AgentTool } from "@mariozechner/pi-ai";
import type { HookRunner } from "./runner.js";
import type { ToolCallEventResult, ToolResultEventResult } from "./types.js";

/**
 * Wrap a tool with hook callbacks.
 * - Emits tool_call event before execution (can block)
 * - Emits tool_result event after execution (can modify result)
 */
export function wrapToolWithHooks<T>(tool: AgentTool<any, T>, hookRunner: HookRunner): AgentTool<any, T> {
	return {
		...tool,
		execute: async (toolCallId: string, params: Record<string, unknown>, signal?: AbortSignal) => {
			// Emit tool_call event - hooks can block execution
			// If hook errors/times out, block by default (fail-safe)
			if (hookRunner.hasHandlers("tool_call")) {
				try {
					const callResult = (await hookRunner.emitToolCall({
						type: "tool_call",
						toolName: tool.name,
						toolCallId,
						input: params,
					})) as ToolCallEventResult | undefined;

					if (callResult?.block) {
						const reason = callResult.reason || "Tool execution was blocked by a hook";
						throw new Error(reason);
					}
				} catch (err) {
					// Hook error or block - throw to mark as error
					if (err instanceof Error) {
						throw err;
					}
					throw new Error(`Hook failed, blocking execution: ${String(err)}`);
				}
			}

			// Execute the actual tool
			const result = await tool.execute(toolCallId, params, signal);

			// Emit tool_result event - hooks can modify the result
			if (hookRunner.hasHandlers("tool_result")) {
				// Extract text from result for hooks
				const resultText = result.content
					.filter((c): c is { type: "text"; text: string } => c.type === "text")
					.map((c) => c.text)
					.join("\n");

				const resultResult = (await hookRunner.emit({
					type: "tool_result",
					toolName: tool.name,
					toolCallId,
					input: params,
					result: resultText,
					isError: false,
				})) as ToolResultEventResult | undefined;

				// Apply modifications if any
				if (resultResult?.result !== undefined) {
					return {
						...result,
						content: [{ type: "text", text: resultResult.result }],
					};
				}
			}

			return result;
		},
	};
}

/**
 * Wrap all tools with hook callbacks.
 */
export function wrapToolsWithHooks<T>(tools: AgentTool<any, T>[], hookRunner: HookRunner): AgentTool<any, T>[] {
	return tools.map((tool) => wrapToolWithHooks(tool, hookRunner));
}
