// SPDX-FileCopyrightText: Amolith <amolith@secluded.site>
// SPDX-FileCopyrightText: Petr Baudis <pasky@ucw.cz>
//
// SPDX-License-Identifier: MIT

import type { ExtensionContext, SessionEntry } from "@mariozechner/pi-coding-agent";
import * as path from "node:path";

/**
 * Model used for handoff extraction calls. Set PI_HANDOFF_MODEL env var
 * as "provider/modelId" (e.g. "anthropic/claude-haiku-4-5") to use a
 * different model for extraction than the session's current model.
 */
const HANDOFF_MODEL_OVERRIDE = process.env.PI_HANDOFF_MODEL;

/**
 * Build a candidate file set from two sources:
 *   1. Primary: actual tool calls (read, write, edit, create) in the session
 *   2. Secondary: file-like patterns in the conversation text (catches files
 *      that were discussed but never opened)
 */
export function extractCandidateFiles(entries: SessionEntry[], conversationText: string): Set<string> {
	const files = new Set<string>();
	const fileToolNames = new Set(["read", "write", "edit", "create"]);

	// Primary: files from actual tool calls
	for (const entry of entries) {
		if (entry.type !== "message") continue;
		const msg = entry.message;
		if (msg.role !== "assistant") continue;

		for (const block of msg.content) {
			if (typeof block !== "object" || block === null || block.type !== "toolCall") continue;
			if (!fileToolNames.has(block.name)) continue;

			const args = block.arguments as Record<string, unknown>;
			const filePath =
				typeof args.path === "string" ? args.path : typeof args.file === "string" ? args.file : undefined;
			if (!filePath) continue;
			if (filePath.endsWith("/SKILL.md")) continue;

			files.add(filePath);
		}
	}

	// Secondary: file-like patterns from conversation text.
	// Trailing lookahead so the boundary isn't consumed — otherwise adjacent
	// files separated by a single space (e.g. "file1.txt file2.txt") get skipped.
	const filePattern = /(?:^|\s)([a-zA-Z0-9._\-/]+\.[a-zA-Z0-9]+)(?=\s|$|[,;:)])/gm;
	for (const match of conversationText.matchAll(filePattern)) {
		const candidate = match[1];
		if (candidate && !candidate.startsWith(".") && candidate.length > 2) {
			files.add(candidate);
		}
	}

	return files;
}

/**
 * Extract skill names that were actually loaded during the conversation.
 * Looks for read() tool calls targeting SKILL.md files and derives the
 * skill name from the parent directory (the convention for pi skills).
 */
export function extractLoadedSkills(entries: SessionEntry[]): string[] {
	const skills = new Set<string>();
	for (const entry of entries) {
		if (entry.type !== "message") continue;
		const msg = entry.message;
		if (msg.role !== "assistant") continue;

		for (const block of msg.content) {
			if (typeof block !== "object" || block === null || block.type !== "toolCall") continue;

			// read() calls where the path ends in SKILL.md
			if (block.name !== "read") continue;
			const args = block.arguments as Record<string, unknown>;
			const filePath = typeof args.path === "string" ? args.path : undefined;
			if (!filePath?.endsWith("/SKILL.md")) continue;

			// Skill name is the parent directory name:
			//   .../skills/backing-up-with-keld/SKILL.md → backing-up-with-keld
			const parent = path.basename(path.dirname(filePath));
			if (parent && parent !== "skills") {
				skills.add(parent);
			}
		}
	}
	return [...skills].sort();
}

/**
 * Resolve the model to use for handoff extraction calls. Uses the
 * PI_HANDOFF_MODEL env var if set, otherwise falls back to the session model.
 */
export function resolveExtractionModel(ctx: {
	model: ExtensionContext["model"];
	modelRegistry: ExtensionContext["modelRegistry"];
}): ExtensionContext["model"] {
	if (!HANDOFF_MODEL_OVERRIDE) return ctx.model;
	const slashIdx = HANDOFF_MODEL_OVERRIDE.indexOf("/");
	if (slashIdx <= 0) return ctx.model;
	const provider = HANDOFF_MODEL_OVERRIDE.slice(0, slashIdx);
	const modelId = HANDOFF_MODEL_OVERRIDE.slice(slashIdx + 1);
	return ctx.modelRegistry.find(provider, modelId) ?? ctx.model;
}
