import assert from "node:assert/strict";
import fs from "node:fs";
import path from "node:path";
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
import { parseFrontmatter, parseFrontmatterList } from "../../src/agents/frontmatter.js";
import registerSubagents from "../../index.js";
import { requestAsyncInterrupt, requestAsyncSteer, requestAsyncStop } from "../../src/runs/background/control-channel.js";
import { verifyRevival } from "./standalone-revival.ts";
import { verifySharedRun } from "./standalone-shared.ts";
import { recoveryDescriptorPath } from "../../src/runs/shared/run-launch-folder.js";

function waitForFile(file: string, predicate: (text: string) => boolean): Promise<void> {
	return new Promise((resolve) => {
		const check = () => {
			if (fs.existsSync(file) && predicate(fs.readFileSync(file, "utf8"))) { fs.unwatchFile(file, check); resolve(); }
		};
		fs.watchFile(file, { interval: 25 }, check);
		check();
	});
}

// Capture only public tool/notification boundaries, never the runner/session factory.
export default function registerSmoke(pi: ExtensionAPI) {
	let tool: Parameters<ExtensionAPI["registerTool"]>[0] | undefined;
	let notifications = 0;
	let notify: (message: unknown) => void = () => {};
	const notification = new Promise<unknown>((resolve) => { notify = resolve; });
	const host = new Proxy(pi, {
		get(target, key) {
			if (key === "registerTool") return (definition: Parameters<ExtensionAPI["registerTool"]>[0]) => {
				target.registerTool(definition);
				if (definition.name === "subagent") tool = definition;
			};
			if (key === "sendMessage") return (...args: Parameters<ExtensionAPI["sendMessage"]>) => {
				target.sendMessage(args[0], { ...args[1], triggerTurn: false });
				if (args[0].customType === "subagent-notify") { notifications++; notify(args[0]); }
			};
			// Drop user messages too, so nothing starts a parent turn.
			if (key === "sendUserMessage") return () => {};
			return Reflect.get(target, key);
		},
	});
	registerSubagents(host);
	pi.on("session_start", async (_event, ctx) => {
		const timeout = setTimeout(() => { console.error("Standalone smoke timed out waiting for parent completion"); process.exit(1); }, 45_000);
		try {
			assert.ok(tool, "the real extension must register its public subagent tool");
			const mode = process.env.PI_STANDALONE_SMOKE_MODE ?? "single";
			assert.ok(["single", "workflow", "targeted-controls", "steer", "interrupt", "stop", "child-stop", "child-timeout", "run-timeout", "tool-timeout", "sdk-init-failure", "persistence-failure", "authorization-failure", "missing-bootstrap", "revival", "shared-run", "parallel-stop"].includes(mode), `unimplemented parent smoke mode: ${mode}`);
			const expectedDeclaredTools = mode === "tool-timeout" ? ["bash"] : [];
			// The default bridge adds upward coordination to nonempty menus, not explicit empty ones.
			const expectedRuntimeTools = mode === "tool-timeout" ? ["bash", "contact_supervisor"] : [];
			const profile = parseFrontmatter(fs.readFileSync("/stage/work/.pi/agents/binary-smoke.md", "utf8"));
			assert.deepEqual(parseFrontmatterList(profile.frontmatter.tools), expectedDeclaredTools);
			const workflow = ["workflow", "targeted-controls", "child-timeout"].includes(mode);
			const expectedState = mode === "interrupt" ? "paused" : mode === "stop" ? "stopped" : mode.endsWith("-timeout") || mode === "child-stop" || mode === "sdk-init-failure" ? "failed" : "complete";
			const workflowScript = mode === "workflow"
				? `const first = await runs.run("first", { agent: "binary-smoke", task: "Return FIRST." }); const siblings = await runs.all([{ key: "left", agent: "binary-smoke", task: "Return LEFT." }, { key: "right", agent: "binary-smoke", task: "Return RIGHT." }]); return { first, siblings };`
				: mode === "targeted-controls"
					? `const left = runs.run("left", { agent: "binary-smoke", task: "Return LEFT." }); const right = runs.run("right", { agent: "binary-smoke", task: "Return RIGHT." }); let interrupted; try { interrupted = await right; } catch (error) { interrupted = String(error); } return { left: await left, right: interrupted };`
					: mode === "child-timeout"
						? `return await runs.run("child-deadline", { agent: "binary-smoke", task: "Wait for cancellation.", timeoutMs: 8000 });`
						: undefined;
			if (workflowScript !== undefined) fs.writeFileSync("/stage/work/smoke-workflow.js", workflowScript);
			const request = workflowScript !== undefined ? { workflow: "/stage/work/smoke-workflow.js" } : { agent: "binary-smoke", task: "Return the scripted response." };
			fs.writeFileSync("/stage/parent-initialized", String(process.pid));
			const guide = await tool.execute("standalone-guide", {}, new AbortController().signal, undefined, ctx);
			assert.match(guide.content.map((part) => part.type === "text" ? part.text : "").join("\n"), /^subagent guide\./);
			if (mode === "shared-run" || mode === "parallel-stop") {
				await verifySharedRun(host, ctx, mode, waitForFile, notification);
				assert.equal(notifications, 1);
				console.log(`PASS standalone native async ${mode}: two SDK sessions, one host, observed shutdown`);
				process.exit(0);
			}
			const startupFailure = mode === "persistence-failure" || mode === "authorization-failure";
			if (startupFailure) assert.equal(JSON.parse(fs.readFileSync("/stage/startup-hook-ready.json", "utf8")).pid, process.pid);
			const launching = tool.execute("standalone-smoke", {
				...request, async: true, model: "standalone-smoke/local", output: false,
				options: { context: "fresh", acceptance: false, timeoutMs: mode === "run-timeout" ? 8000 : 20000, ...(mode === "tool-timeout" ? { toolTimeoutMs: 1000 } : {}) },
			}, new AbortController().signal, undefined, ctx);
			if (mode === "missing-bootstrap") {
				assert.ok(fs.existsSync("/stage/withheld-binary-bootstrap.js"));
				await assert.rejects(launching, /Background runner bootstrap not found/);
				console.log("PASS standalone native async missing-bootstrap: missing asset rejected at the public launch boundary");
				process.exit(0);
			}
			if (startupFailure) {
				await assert.rejects(launching, (error: unknown) => {
					assert.ok(error instanceof Error);
					assert.match(error.message, new RegExp(`fixture ${mode} after real spawn`));
					fs.writeFileSync("/stage/launch-error.json", JSON.stringify({ message: error.message }));
					return true;
				});
				assert.ok(fs.existsSync("/stage/failure-injected.json"));
				const injected = JSON.parse(fs.readFileSync("/stage/failure-injected.json", "utf8"));
				assert.equal(injected.mode, mode);
				const terminalPath = `${injected.asyncDir}/process-terminal.json`;
				await waitForFile(terminalPath, (text) => JSON.parse(text).instances?.some((instance: { kind: string; closeObservedAt?: number }) => instance.kind === "runner" && typeof instance.closeObservedAt === "number"));
				assert.throws(() => process.kill(injected.pid, 0), { code: "ESRCH" });
				const status = JSON.parse(fs.readFileSync(`${injected.asyncDir}/status.json`, "utf8"));
				assert.equal(status.state, "failed");
				assert.equal(fs.existsSync(`${injected.asyncDir}/runner-startup-proceed.json`), false);
				const events = fs.readFileSync("/stage/lifecycle.jsonl", "utf8").trim().split("\n").map((line) => JSON.parse(line));
				assert.ok(events.every((event) => event.pid === process.pid), "positive-control task must never enter a child session");
				fs.writeFileSync("/stage/startup-failure.json", JSON.stringify({ ...injected, mode, terminal: JSON.parse(fs.readFileSync(terminalPath, "utf8")) }, null, 2));
				console.log(`PASS standalone native async ${mode}: post-spawn fault observed, no proceed, no child work, runner close observed`);
				process.exit(0);
			}
			const launch = await launching;
			fs.writeFileSync("/stage/launch.json", JSON.stringify(launch, null, 2));
			assert.notEqual(launch.isError, true, JSON.stringify(launch));
			const details = launch.details as { asyncDir: string; asyncId: string };
			if (["steer", "interrupt", "stop", "child-stop"].includes(mode)) {
				await waitForFile("/stage/lifecycle.jsonl", (text) => text.includes('"event":"request"'));
				if (mode === "interrupt") requestAsyncInterrupt(details.asyncDir);
				if (mode === "stop") requestAsyncStop(details.asyncDir);
				if (mode === "child-stop") requestAsyncStop(details.asyncDir, { targetIndex: 0, childId: "step:0" });
				if (mode === "steer") {
					requestAsyncSteer(details.asyncDir, { id: "binary-steer", targetIndex: 0, message: "UNIQUE_STEERING_MARKER" });
					await waitForFile(`${details.asyncDir}/events.jsonl`, (text) => text.includes('"type":"subagent.steer.queued"'));
					fs.writeFileSync("/stage/release", "go");
				}
			}
			if (mode === "targeted-controls") {
				await waitForFile("/stage/lifecycle.jsonl", (text) => text.split("\n").filter((line) => line.includes('"event":"request"')).length === 2);
				const active = JSON.parse(fs.readFileSync(`${details.asyncDir}/status.json`, "utf8"));
				const [left, right] = active.steps.map((step: { runId: string }) => path.join(path.dirname(details.asyncDir), step.runId));
				requestAsyncSteer(left, { id: "binary-steer", targetIndex: 0, message: "UNIQUE_STEERING_MARKER" });
				await waitForFile(`${left}/events.jsonl`, (text) => text.includes('"type":"subagent.steer.queued"'));
				requestAsyncInterrupt(right);
				await waitForFile(`${right}/status.json`, (text) => JSON.parse(text).state === "paused");
				assert.equal(JSON.parse(fs.readFileSync(`${left}/status.json`, "utf8")).state, "running", "interrupting right must leave left running");
				fs.writeFileSync("/stage/release", "go");
			}
			if (mode === "tool-timeout") await waitForFile("/stage/tool.pid", (text) => /^\d+\s*$/.test(text));
			const completed = await notification;
			fs.writeFileSync("/stage/notification.json", JSON.stringify(completed, null, 2));
			if (expectedState === "complete") assert.match(JSON.stringify(completed), /standalone child response verified/);
			const parentStatus = JSON.parse(fs.readFileSync(`${details.asyncDir}/status.json`, "utf8"));
			const runIds: string[] = workflow ? parentStatus.steps.map((step: { runId: string }) => step.runId) : [details.asyncId];
			assert.equal(new Set(runIds).size, mode === "workflow" ? 3 : mode === "targeted-controls" ? 2 : 1);
			for (const runId of runIds) {
				const runDir = path.join(path.dirname(details.asyncDir), runId);
				const terminalPath = `${runDir}/process-terminal.json`;
				const atNotification = JSON.parse(fs.readFileSync(terminalPath, "utf8"));
				fs.appendFileSync("/stage/notification-terminal.jsonl", JSON.stringify({ runId, state: atNotification.state }) + "\n");
				// Notification can precede disposal: read shutdown evidence only after observed close.
				if (atNotification.state !== "observed") await waitForFile(terminalPath, (text) => JSON.parse(text).state === "observed");
				const terminal = JSON.parse(fs.readFileSync(terminalPath, "utf8"));
				assert.equal(terminal.state, "observed");
				assert.equal(terminal.runId, runId);
				assert.ok(terminal.instances.some((instance: { kind: string; exitCode: number }) => instance.kind === "runner" && instance.exitCode === 0));
				const lifecycle = fs.readFileSync("/stage/lifecycle.jsonl", "utf8").trim().split("\n").map((line) => JSON.parse(line));
				const status = JSON.parse(fs.readFileSync(`${runDir}/status.json`, "utf8"));
				const runState = mode === "targeted-controls" && runIds.indexOf(runId) === 1 ? "paused" : expectedState;
				const steered = mode === "steer" || (mode === "targeted-controls" && runIds.indexOf(runId) === 0);
				assert.equal(status.state, runState, JSON.stringify(status));
				if (mode === "stop" || mode === "child-stop") assert.equal(status.steps[0].status, "stopped");
				assert.equal(status.sessionId, ctx.sessionManager.getSessionId());
				assert.equal(status.completionOwnerId, parentStatus.completionOwnerId);
				assert.equal(status.steps[0].model, "standalone-smoke/local");
				const descriptor = JSON.parse(fs.readFileSync(recoveryDescriptorPath(path.dirname(runDir), path.basename(runDir)), "utf8"));
				assert.deepEqual(descriptor.tools, expectedDeclaredTools);
				const observed = fs.readFileSync("/stage/bootstrap-observer.jsonl", "utf8").trim().split("\n").map((line) => JSON.parse(line)).filter((event) => event.pid === status.pid);
				assert.deepEqual(observed.map((event) => event.event), mode === "sdk-init-failure" ? ["observer-ready"] : ["observer-ready", "session-start"]);
				if (mode === "sdk-init-failure") {
					assert.equal(JSON.parse(fs.readFileSync("/stage/provider-failure.json", "utf8")).pid, status.pid);
					assert.match(status.error, /Model "standalone-smoke\/local" not found/);
				} else {
					const starts = lifecycle.filter((entry) => entry.event === "start" && entry.pid === status.pid);
					assert.equal(starts.length, 1, "outer bootstrap must not start an extra provider session");
					assert.deepEqual(starts[0].tools, expectedRuntimeTools);
					assert.equal(starts[0].provider, "standalone-smoke");
					assert.equal(starts[0].model, "local");
					if (runState === "complete") {
						assert.ok(fs.existsSync(status.steps[0].sessionFile));
						const output = fs.readFileSync(status.steps[0].transcriptPath, "utf8");
						assert.match(output, /standalone child response verified/);
						if (mode === "workflow") assert.ok(output.includes(["FIRST", "LEFT", "RIGHT"][runIds.indexOf(runId)]));
					}
					if (mode.endsWith("-timeout")) {
						assert.equal(status.steps[0].timedOut, true);
						assert.match(status.steps[0].error, mode === "tool-timeout" ? /Tool 'bash' exceeded its timeout of 1000ms/ : /timed out after 8000ms/);
					}
					if (steered) {
						const followup = lifecycle.find((entry) => entry.event === "followup" && entry.pid === status.pid);
						assert.ok(followup && JSON.stringify(followup.messages).includes("UNIQUE_STEERING_MARKER"));
					}
					const child = lifecycle.find((entry) => entry.event === "shutdown" && entry.pid === status.pid && entry.calls === (steered ? 2 : 1));
					assert.ok(child, "each real run must prompt and shut down its child session");
				}
				assert.throws(() => process.kill(status.pid, 0), { code: "ESRCH" });
			}
			if (mode === "tool-timeout") {
				const toolPid = Number(fs.readFileSync("/stage/tool.pid", "utf8"));
				assert.throws(() => process.kill(toolPid, 0), { code: "ESRCH" });
			}
			assert.equal(notifications, 1, "one logical parent completion, not an extra bootstrap completion");
			if (mode === "revival") {
				const deliveries: Record<string, number> = {};
				await verifyRevival(tool, ctx, details, waitForFile, (text) => new Promise<void>((resolve) => {
					deliveries[text] = 0;
					notify = (message) => {
						if (JSON.stringify(message).includes(text)) {
							deliveries[text]++;
							fs.appendFileSync("/stage/revival-notifications.jsonl", JSON.stringify(message) + "\n");
							resolve();
						}
					};
				}));
				assert.deepEqual(Object.values(deliveries), [1, 1]);
			}
			console.log(`PASS standalone native async ${mode}: ${runIds.length} run(s), checked SDK lifecycle, observed exits, parent notification`);
			process.exit(0);
		} catch (error) {
			console.error(error);
			process.exit(1);
		} finally {
			clearTimeout(timeout);
		}
	});
}
