/**
 * context-board-tool.js — Executor for the context_board tool.
 *
 * Two operations: add (add an invariant) and prune (remove by id).
 * The `repository` and `branch` fields are auto-filled from the git context
 * provided by the caller — the LLM never sets them directly.
 *
 * Pattern mirrors memory-tools.js: pure executor functions that the bootstrap
 * registration layer wraps in pi.registerTool().
 */
import { addBoardEntry, pruneBoardEntry } from "../context-board.js";
import { isDbAvailable } from "../sf-db.js";

function dbUnavailable(operation) {
	return {
		content: [
			{
				type: "text",
				text: "Error: SF database is not available. context_board requires an initialized .sf/ project.",
			},
		],
		details: { operation, error: "db_unavailable" },
		isError: true,
	};
}

/**
 * Execute a context_board add operation.
 *
 * @param {object} params
 * @param {string} params.content    — Invariant text.
 * @param {string} [params.category] — Optional category label.
 * @param {string} params.repository — Resolved by caller from git context.
 * @param {string} params.branch     — Resolved by caller from git context.
 */
export function executeContextBoardAdd(params) {
	if (!isDbAvailable()) return dbUnavailable("context_board_add");

	const content = (params.content ?? "").trim();
	if (!content) {
		return {
			content: [{ type: "text", text: "Error: content is required." }],
			details: { operation: "context_board_add", error: "missing_content" },
			isError: true,
		};
	}

	const id = addBoardEntry({
		content,
		category: params.category ?? null,
		repository: params.repository ?? "",
		branch: params.branch ?? "",
	});

	if (!id) {
		return {
			content: [{ type: "text", text: "Error: failed to add board entry." }],
			details: { operation: "context_board_add", error: "insert_failed" },
			isError: true,
		};
	}

	const catLabel = params.category ? ` [${params.category}]` : "";
	return {
		content: [
			{
				type: "text",
				text: `Board entry added: ${id}${catLabel}\n${content}\n\nThis invariant will appear in every subsequent system prompt for ${params.branch ?? "(current branch)"}.`,
			},
		],
		details: {
			operation: "context_board_add",
			id,
			category: params.category ?? null,
			repository: params.repository,
			branch: params.branch,
		},
	};
}

/**
 * Execute a context_board prune operation.
 *
 * @param {object} params
 * @param {string} params.id — Entry id to remove.
 */
export function executeContextBoardPrune(params) {
	if (!isDbAvailable()) return dbUnavailable("context_board_prune");

	const id = (params.id ?? "").trim();
	if (!id) {
		return {
			content: [{ type: "text", text: "Error: id is required." }],
			details: { operation: "context_board_prune", error: "missing_id" },
			isError: true,
		};
	}

	const removed = pruneBoardEntry(id);
	if (!removed) {
		return {
			content: [
				{
					type: "text",
					text: `No board entry found with id "${id}". It may have already been pruned.`,
				},
			],
			details: { operation: "context_board_prune", id, removed: false },
		};
	}

	return {
		content: [
			{
				type: "text",
				text: `Board entry "${id}" pruned. It will no longer appear in system prompts.`,
			},
		],
		details: { operation: "context_board_prune", id, removed: true },
	};
}
