import type { ResolvedToolBudget, ToolBudgetConfig, ToolBudgetState } from "../../shared/types.ts";

export const DEFAULT_TOOL_BUDGET_BLOCK = ["read", "grep", "find", "ls"] as const;

export function normalizeToolBudgetBlock(block: ToolBudgetConfig["block"] | undefined): "*" | string[] {
	if (block === "*") return "*";
	if (block === undefined) return [...DEFAULT_TOOL_BUDGET_BLOCK];
	return [...new Set(block.map((tool) => tool.trim()).filter(Boolean))];
}

export function validateToolBudgetConfig(
	raw: unknown,
	label = "toolBudget",
	options: { minimumHard?: 0 | 1 } = {},
): { budget?: ResolvedToolBudget; error?: string } {
	if (raw === undefined) return {};
	if (!raw || typeof raw !== "object" || Array.isArray(raw)) return { error: `${label} must be an object with hard and optional soft/block.` };
	const value = raw as ToolBudgetConfig;
	const minimumHard = options.minimumHard ?? 1;
	if (typeof value.hard !== "number" || !Number.isInteger(value.hard) || value.hard < minimumHard) {
		return { error: `${label}.hard must be an integer >= ${minimumHard}.` };
	}
	if (value.soft !== undefined && (typeof value.soft !== "number" || !Number.isInteger(value.soft) || value.soft < 1)) {
		return { error: `${label}.soft must be an integer >= 1 when provided.` };
	}
	if (value.soft !== undefined && value.soft > value.hard) {
		return { error: `${label}.soft must be <= ${label}.hard.` };
	}
	if (value.block !== undefined && value.block !== "*") {
		if (!Array.isArray(value.block)) return { error: `${label}.block must be "*" or an array of tool names.` };
		if (value.block.length === 0) return { error: `${label}.block must contain at least one tool name.` };
		for (const item of value.block) {
			if (typeof item !== "string" || !item.trim()) return { error: `${label}.block must contain non-empty tool names.` };
		}
	}
	return { budget: { hard: value.hard, ...(value.soft !== undefined ? { soft: value.soft } : {}), block: normalizeToolBudgetBlock(value.block) } };
}

export function initialToolBudgetState(budget: ResolvedToolBudget): ToolBudgetState {
	return { ...budget, toolCount: 0, outcome: "within-budget" };
}

export function toolBudgetState(budget: ResolvedToolBudget, toolCount: number, blockedTool?: string): ToolBudgetState {
	const overHard = toolCount > budget.hard;
	const overSoft = budget.soft !== undefined && toolCount >= budget.soft;
	return {
		...budget,
		toolCount,
		outcome: overHard ? (blockedTool ? "hard-blocked" : "hard-reached") : overSoft ? "soft-reached" : "within-budget",
		...(overSoft ? { softReachedAt: budget.soft } : {}),
		...(overHard ? { hardReachedAt: budget.hard, blockedTool } : {}),
	};
}

export function shouldBlockToolForBudget(budget: ResolvedToolBudget, toolName: string, nextToolCount: number): boolean {
	if (nextToolCount <= budget.hard) return false;
	return budget.block === "*" || budget.block.includes(toolName);
}

export function toolBudgetSoftNudge(budget: ResolvedToolBudget, toolCount: number): string {
	return `Tool budget soft limit reached after ${toolCount} tool call${toolCount === 1 ? "" : "s"} (soft ${budget.soft}, hard ${budget.hard}). Stop starting new browsing/search work and finalize from the context you already have.`;
}

export function toolBudgetBlockedMessage(budget: ResolvedToolBudget, toolName: string, toolCount: number): string {
	return `Tool budget hard limit reached after ${toolCount} tool call${toolCount === 1 ? "" : "s"} (hard ${budget.hard}). The '${toolName}' tool is blocked so you can finalize from the context you already have.`;
}

const TOOL_BUDGET_BLOCKED_MESSAGE = /^Tool budget hard limit reached after (\d+) tool calls? \(hard (\d+)\)\. The '([^']+)' tool is blocked so you can finalize from the context you already have\.$/;

/**
 * Recognizes a hard-block reason generated by toolBudgetBlockedMessage for this exact budget
 * and tool. A tool result only counts as a budget block when the entire message matches the
 * runtime format and the embedded hard limit and tool name belong to this run; incidental
 * occurrences of the phrase inside ordinary tool output are rejected.
 */
export function isToolBudgetBlockedMessage(
	budget: ResolvedToolBudget,
	resultText: string,
	blockedTool: string | undefined,
): boolean {
	const tool = blockedTool?.trim();
	if (!tool) return false;
	const match = TOOL_BUDGET_BLOCKED_MESSAGE.exec(resultText.trim());
	if (!match) return false;
	const [, blockedToolCount, hard, namedTool] = match;
	return Number(hard) === budget.hard && Number(blockedToolCount) > budget.hard && namedTool === tool;
}
