import assert from "node:assert/strict";
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import { afterEach, beforeEach, describe, it } from "node:test";
import { discoverPromptWorkflows, registerPromptWorkflowCommands } from "../../src/slash/prompt-workflows.ts";
import { runWorkflowScript } from "../../src/workflows/scripted-workflow.ts";
import type { SubagentParamsLike } from "../../src/runs/foreground/subagent-executor.ts";

const originalAgentDir = process.env.PI_CODING_AGENT_DIR;

function writePrompt(dir: string, name: string, content: string): void {
	fs.mkdirSync(dir, { recursive: true });
	fs.writeFileSync(path.join(dir, `${name}.md`), content, "utf-8");
}

function makeCtx(cwd: string) {
	return {
		cwd,
		hasUI: true,
		ui: {
			notifications: [] as Array<{ message: string; level: string }>,
			notify(message: string, level: string) {
				this.notifications.push({ message, level });
			},
		},
	} as never;
}

describe("prompt workflows", () => {
	let tempDir = "";
	let agentDir = "";
	let cwd = "";

	beforeEach(() => {
		tempDir = fs.mkdtempSync(path.join(os.tmpdir(), "pi-prompt-workflows-"));
		agentDir = path.join(tempDir, "agent");
		cwd = path.join(tempDir, "repo");
		fs.mkdirSync(cwd, { recursive: true });
		process.env.PI_CODING_AGENT_DIR = agentDir;
	});

	afterEach(() => {
		if (originalAgentDir === undefined) delete process.env.PI_CODING_AGENT_DIR;
		else process.env.PI_CODING_AGENT_DIR = originalAgentDir;
		fs.rmSync(tempDir, { recursive: true, force: true });
	});

	it("discovers project workflows over user workflows", () => {
		writePrompt(path.join(agentDir, "prompts"), "native-test", `---
description: User version
subagent: reviewer
---
User body
`);
		writePrompt(path.join(cwd, ".pi", "prompts"), "native-test", `---
description: Project version
subagent: worker
model: openai/gpt-5-mini
---
Project body $1
`);

		const workflow = discoverPromptWorkflows(cwd).find((entry) => entry.name === "native-test");

		assert.equal(workflow?.description, "Project version");
		assert.equal(workflow?.agent, "worker");
		assert.equal(workflow?.model, "openai/gpt-5-mini");
	});

	it("discovers symlinked prompt files and skips dangling links", () => {
		const promptsDir = path.join(agentDir, "prompts");
		writePrompt(path.join(tempDir, "dotfiles"), "linked", `---
description: Linked prompt
---
Say hi
`);
		fs.mkdirSync(promptsDir, { recursive: true });
		fs.symlinkSync(path.join(tempDir, "dotfiles", "linked.md"), path.join(promptsDir, "linked.md"));
		fs.symlinkSync(path.join(tempDir, "dotfiles", "missing.md"), path.join(promptsDir, "dangling.md"));

		const names = discoverPromptWorkflows(cwd).map((entry) => entry.name);

		assert.ok(names.includes("linked"), names.join(", "));
		assert.equal(names.includes("dangling"), false);

		if (process.platform !== "win32") {
			fs.symlinkSync(path.join(promptsDir, "loop.md"), path.join(promptsDir, "loop.md"));
			assert.throws(() => discoverPromptWorkflows(cwd), { code: "ELOOP" });
		}
	});

	it("runs a named workflow through native subagent execution", async () => {
		writePrompt(path.join(cwd, ".pi", "prompts"), "native-run", `---
description: Run native prompt
subagent: reviewer
model: anthropic/claude-sonnet-4
skill: deslop,typescript-code
---
Review $1 with $ARGUMENTS
`);
		const commands = new Map<string, { handler: (args: string, ctx: never) => Promise<void> }>();
		const sent: unknown[] = [];
		const runs: SubagentParamsLike[] = [];
		registerPromptWorkflowCommands({
			pi: {
				registerCommand: (name: string, command: { handler: (args: string, ctx: never) => Promise<void> }) => commands.set(name, command),
				sendMessage: (message: unknown) => sent.push(message),
			} as never,
			run: async (params) => { runs.push(params); },
		});

		await commands.get("prompt-workflow")!.handler('native-run target --fork', makeCtx(cwd));

		assert.equal(sent.length, 0);
		assert.equal(runs.length, 1);
		const params = runs[0];
		assert.equal(params?.agent, undefined);
		assert.equal(params?.task, undefined);
		assert.equal(params?.clarify, undefined);
		assert.equal(params?.model, undefined);
		assert.equal(params?.skill, undefined);
		assert.equal(params?.context, undefined);
		assert.equal(params?.agentScope, "both");
		assert.equal(params?.async, false);
		const script = params?.workflowScript ?? "";
		assert.match(script, /runs\.run\("prompt-1-native-run"/);
		assert.match(script, /"agent":"reviewer"/);
		assert.match(script, /"model":"anthropic\/claude-sonnet-4"/);
		assert.match(script, /"skill":\["deslop","typescript-code"\]/);
		assert.match(script, /"context":"fork"/);
		assert.match(script, /Review target with target/);
	});

	it("runs declared prompt sequences through workflowScript", async () => {
		writePrompt(path.join(cwd, ".pi", "prompts"), "native-analyze", `---
description: Analyze
subagent: scout
chain: native-analyze -> native-fix
---
Analyze $@
`);
		writePrompt(path.join(cwd, ".pi", "prompts"), "native-fix", `---
description: Fix
subagent: worker
---
Fix from {previous}: $@
`);
		const commands = new Map<string, { handler: (args: string, ctx: never) => Promise<void> }>();
		const runs: SubagentParamsLike[] = [];
		registerPromptWorkflowCommands({
			pi: {
				registerCommand: (name: string, command: { handler: (args: string, ctx: never) => Promise<void> }) => commands.set(name, command),
				sendMessage: () => {},
			} as never,
			run: async (params) => { runs.push(params); },
		});

		await commands.get("prompt-workflow")!.handler("native-analyze bug report", makeCtx(cwd));

		assert.equal(runs.length, 1);
		const params = runs[0];
		assert.equal(params?.agent, undefined);
		assert.equal(params?.task, undefined);
		assert.equal(params?.clarify, undefined);
		assert.equal(params?.agentScope, "both");
		assert.equal(params?.async, false);
		const script = params?.workflowScript ?? "";
		assert.match(script, /runs\.run\("prompt-1-native-analyze"/);
		assert.match(script, /runs\.run\("prompt-2-native-fix"/);
		assert.match(script, /replaceAll\("\{previous\}"/);
		assert.equal(commands.has("chain-prompts"), false);

		const launches: Array<{ key: string; task: unknown }> = [];
		const executed = await runWorkflowScript({
			script,
			async launch(key, childParams) {
				launches.push({ key, task: childParams.task });
				return { key, ok: true, output: key === "prompt-1-native-analyze" ? "analysis output" : "fixed output", artifactPaths: [] };
			},
			async status(key) { return { key, ok: true, output: "ok", artifactPaths: [] }; },
		});
		assert.deepEqual(launches, [
			{ key: "prompt-1-native-analyze", task: "Analyze bug report" },
			{ key: "prompt-2-native-fix", task: "Fix from analysis output: bug report" },
		]);
		assert.equal(executed.value, "fixed output");
	});
});
