/**
 * VectorDrive Info Tool
 *
 * Introspects the vectordrive native package status, version, implementation
 * type (native vs wasm), and vector count.
 */

import { Type } from "@sinclair/typebox";
import type { ExtensionAPI } from "@singularity-forge/pi-coding-agent";
import { VectordriveManager } from "./manager.js";

export interface ToolExecutionResult {
	content: Array<{ type: "text"; text: string }>;
	details: Record<string, unknown>;
	isError?: boolean;
}

export function registerVectordriveInfoTool(pi: ExtensionAPI): void {
	pi.registerTool({
		name: "vectordrive_info",
		label: "VectorDrive Info",
		description:
			"Check VectorDrive native vector database status. " +
			"Returns implementation type (native Rust or WASM), version, " +
			"vector count, and database path.",
		promptSnippet: "Check VectorDrive database status and capabilities",
		parameters: Type.Object({
			refresh: Type.Optional(
				Type.Boolean({
					default: false,
					description: "Force re-probe instead of using cached status",
				}),
			),
		}),
		async execute(_toolCallId, params, _signal, _onUpdate, _ctx) {
			const manager = VectordriveManager.getInstance();

			if (params.refresh) {
				await manager.close();
			}

			const status = await manager.getStatus();

			const lines: string[] = [];
			lines.push(`# VectorDrive Status`);
			lines.push("");
			lines.push(`- **Backend**: ${status.backend}`);
			lines.push(`- **Implementation**: ${status.implementation ?? "n/a"}`);
			lines.push(`- **Version**: ${status.version ?? "n/a"}`);
			lines.push(`- **Vectors**: ${status.vectorCount}`);
			lines.push(`- **Initialized**: ${status.initialized}`);
			if (status.dbPath) {
				lines.push(`- **DB Path**: ${status.dbPath}`);
			}
			if (status.error) {
				lines.push(`- **Error**: ${status.error}`);
			}

			const text = lines.join("\n");

			return {
				content: [{ type: "text", text }],
				details: { status },
				isError: status.backend === "none",
			};
		},
	});
}
