/**
 * System prompt construction and project context loading
 */

import { getDocsPath, getExamplesPath, getReadmePath } from "../config.js";
import { toPosixPath } from "../utils/path-display.js";
import { formatSkillsForPrompt, type Skill } from "./skills.js";

/** Tool descriptions for system prompt */
const toolDescriptions: Record<string, string> = {
	Read: "Read file contents",
	Bash: "Execute bash commands (ls, grep, find, etc.)",
	Edit: "Make surgical edits to files (find exact text and replace)",
	Write: "Create or overwrite files",
	Grep: "Search file contents for patterns (respects .gitignore)",
	Glob: "Find files by glob pattern (respects .gitignore)",
	LS: "List directory contents",
	lsp: "Code intelligence via Language Server Protocol (go-to-definition, references, diagnostics, hover, rename, symbols)",
};

export interface BuildSystemPromptOptions {
	/** Custom system prompt (replaces default). */
	customPrompt?: string;
	/** Tools to include in prompt. Default: [Read, Grep, Glob, LS, Bash, Edit, Write, lsp] */
	selectedTools?: string[];
	/** Optional one-line tool snippets keyed by tool name. */
	toolSnippets?: Record<string, string>;
	/** Additional guideline bullets appended to the default system prompt guidelines. */
	promptGuidelines?: string[];
	/** Text to append to system prompt. */
	appendSystemPrompt?: string;
	/** Working directory. Default: process.cwd() */
	cwd?: string;
	/** Pre-loaded context files. */
	contextFiles?: Array<{ path: string; content: string }>;
	/** Pre-loaded skills. */
	skills?: Skill[];
	/**
	 * Optional predicate applied to the `skills` list before rendering the
	 * <available_skills> catalog. Returning `false` omits a skill from the
	 * prompt (the skill remains loaded and invocable by name — only the
	 * catalog listing is suppressed).
	 *
	 * Intended for consumers that can narrow the relevant skill surface
	 * (e.g. per-unit-type manifests) to reduce cached system-prompt bloat.
	 * When omitted, all non-`disableModelInvocation` skills render — i.e.
	 * behavior is unchanged from before this option existed.
	 *
	 * Contract: the predicate must be **pure and synchronous**. It may be
	 * invoked on every system-prompt rebuild (tool-set changes and
	 * runtime resource-loader extensions both trigger one), so any state
	 * the closure captures should be stable across the rebuild window.
	 * If the predicate throws, `buildSystemPrompt` logs a warning and
	 * falls back to the unfiltered skill list — callers never see the
	 * exception and the session stays consistent.
	 */
	skillFilter?: (skill: Skill) => boolean;
}

/** Build the system prompt with tools, guidelines, and context */
export function buildSystemPrompt(
	options: BuildSystemPromptOptions = {},
): string {
	const {
		customPrompt,
		selectedTools,
		toolSnippets,
		promptGuidelines,
		appendSystemPrompt,
		cwd,
		contextFiles: providedContextFiles,
		skills: providedSkills,
		skillFilter,
	} = options;
	const resolvedCwd = toPosixPath(cwd ?? process.cwd());

	const now = new Date();
	const dateTime = now.toLocaleString("en-US", {
		weekday: "long",
		year: "numeric",
		month: "long",
		day: "numeric",
		hour: "2-digit",
		minute: "2-digit",
		second: "2-digit",
		timeZoneName: "short",
	});

	const appendSection = appendSystemPrompt ? `\n\n${appendSystemPrompt}` : "";

	const contextFiles = providedContextFiles ?? [];
	const skillsBase = providedSkills ?? [];
	let skills = skillsBase;
	if (skillFilter) {
		try {
			skills = skillsBase.filter(skillFilter);
		} catch (error) {
			// A consumer's predicate threw. Fall back to the unfiltered list so
			// the session stays consistent — callers (e.g. AgentSession.setTools)
			// must not be left with updated tools but a stale system prompt.
			const message = error instanceof Error ? error.message : String(error);
			console.warn(
				`buildSystemPrompt: skillFilter threw; falling back to unfiltered skills. Error: ${message}`,
			);
			skills = skillsBase;
		}
	}

	if (customPrompt) {
		let prompt = customPrompt;

		if (appendSection) {
			prompt += appendSection;
		}

		// Append project context files
		if (contextFiles.length > 0) {
			prompt += "\n\n# Project Context\n\n";
			prompt += "Project-specific instructions and guidelines:\n\n";
			for (const { path: filePath, content } of contextFiles) {
				prompt += `## ${filePath}\n\n${content}\n\n`;
			}
		}

		// Append skills section (if Read or Skill tool is available)
		const customPromptHasSkillAccess =
			!selectedTools ||
			selectedTools.includes("Read") ||
			selectedTools.includes("Skill");
		if (customPromptHasSkillAccess && skills.length > 0) {
			prompt += formatSkillsForPrompt(skills);
		}

		// Add date/time and working directory last
		prompt += `\nCurrent date and time: ${dateTime}`;
		prompt += `\nCurrent working directory: ${resolvedCwd}`;

		// Append promptGuidelines from extension-registered tools.
		// Without this, tools registered via pi.registerTool() with promptGuidelines
		// have their definitions reach the API but the model has no guidance on when
		// to use them (#1184).
		if (promptGuidelines && promptGuidelines.length > 0) {
			prompt += "\n\n";
			for (const guideline of promptGuidelines) {
				prompt += guideline + "\n";
			}
		}

		return prompt;
	}

	// Get absolute paths to documentation and examples
	const readmePath = getReadmePath();
	const docsPath = getDocsPath();
	const examplesPath = getExamplesPath();

	// Build tools list based on selected tools.
	// Built-ins use toolDescriptions. Custom tools can provide one-line snippets.
	const tools = selectedTools || [
		"Read",
		"Grep",
		"Glob",
		"LS",
		"Bash",
		"Edit",
		"Write",
		"lsp",
	];
	const toolsList =
		tools.length > 0
			? tools
					.map((name) => {
						const snippet =
							toolSnippets?.[name] ?? toolDescriptions[name] ?? name;
						return `- ${name}: ${snippet}`;
					})
					.join("\n")
			: "(none)";

	// Build guidelines based on which tools are actually available
	const guidelinesList: string[] = [];
	const guidelinesSet = new Set<string>();
	const addGuideline = (guideline: string): void => {
		if (guidelinesSet.has(guideline)) {
			return;
		}
		guidelinesSet.add(guideline);
		guidelinesList.push(guideline);
	};

	const hasBash = tools.includes("Bash");
	const hasEdit = tools.includes("Edit");
	const hasWrite = tools.includes("Write");
	const hasGrep = tools.includes("Grep");
	const hasFind = tools.includes("Glob");
	const hasLs = tools.includes("LS");
	const hasRead = tools.includes("Read");
	const hasLsp = tools.includes("lsp");

	// File exploration guidelines
	if (hasBash && !hasGrep && !hasFind && !hasLs) {
		addGuideline("Use Bash for file operations like ls, rg, find");
	} else if (hasBash && (hasGrep || hasFind || hasLs)) {
		addGuideline(
			"Prefer Grep/Glob/LS tools over Bash for file exploration (faster, respects .gitignore)",
		);
	}

	// Read before edit guideline
	if (hasRead && hasEdit) {
		addGuideline(
			"Use Read to examine files before editing. You must use this tool instead of cat or sed.",
		);
	}

	// Edit guideline
	if (hasEdit) {
		addGuideline("Use Edit for precise changes (old text must match exactly)");
	}

	// Write guideline
	if (hasWrite) {
		addGuideline("Use Write only for new files or complete rewrites");
	}

	// LSP guideline
	if (hasLsp) {
		addGuideline(
			`Use lsp as the primary tool for code navigation in typed codebases:
- Navigation: definition, type_definition, implementation, references, incoming_calls, outgoing_calls
- Understanding: hover (types + docs), signature (parameter info), symbols (file/workspace search)
- Refactoring: rename (project-wide), code_actions (quick-fixes, imports, refactors), format (formatter)
- Verification: diagnostics after edits to catch type errors immediately
- Never grep for a symbol definition when lsp can resolve it semantically
- Never shell out to a formatter when lsp format is available`,
		);
	}

	// Output guideline (only when actually writing or executing)
	if (hasEdit || hasWrite) {
		addGuideline(
			"When summarizing your actions, output plain text directly - do NOT use cat or bash to display what you did",
		);
	}

	for (const guideline of promptGuidelines ?? []) {
		const normalized = guideline.trim();
		if (normalized.length > 0) {
			addGuideline(normalized);
		}
	}

	// Always include these
	addGuideline("Be concise in your responses");
	addGuideline("Show file paths clearly when working with files");

	const guidelines = guidelinesList.map((g) => `- ${g}`).join("\n");

	let prompt = `You are a purpose-driven software compiler. You take bounded intent, produce a falsifiable purpose contract (the PDD/TDD gate), then write failing tests before implementation. You help users by reading files, executing commands, editing code, and writing new files.

Available tools:
${toolsList}

In addition to the tools above, you may have access to other custom tools depending on the project.

Guidelines:
${guidelines}

Pi documentation (read only when the user asks about pi itself, its SDK, extensions, themes, skills, or TUI):
- Main documentation: ${readmePath}
- Additional docs: ${docsPath}
- Examples: ${examplesPath} (extensions, custom tools, SDK)
- When asked about: extensions (docs/extensions.md, examples/extensions/), themes (docs/themes.md), skills (docs/skills.md), prompt templates (docs/prompt-templates.md), TUI components (docs/tui.md), keybindings (docs/keybindings.md), SDK integrations (docs/sdk.md), custom providers (docs/custom-provider.md), adding models (docs/models.md), pi packages (docs/packages.md)
- When working on pi topics, read the docs and examples, and follow .md cross-references before implementing
- Always read pi .md files completely and follow links to related docs (e.g., tui.md for TUI API details)`;

	if (appendSection) {
		prompt += appendSection;
	}

	// Append project context files
	if (contextFiles.length > 0) {
		prompt += "\n\n# Project Context\n\n";
		prompt += "Project-specific instructions and guidelines:\n\n";
		for (const { path: filePath, content } of contextFiles) {
			prompt += `## ${filePath}\n\n${content}\n\n`;
		}
	}

	// Append skills section (if read or Skill tool is available)
	const hasSkill = tools.includes("Skill");
	if ((hasRead || hasSkill) && skills.length > 0) {
		prompt += formatSkillsForPrompt(skills);
	}

	// Add date/time and working directory last
	prompt += `\nCurrent date and time: ${dateTime}`;
	prompt += `\nCurrent working directory: ${resolvedCwd}`;

	return prompt;
}
