import assert from "node:assert/strict";
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { afterEach, test } from "vitest";
import {
	renderLiveStatus,
	resolveRecoveryPick,
	runStatusCli,
} from "../cli-status.ts";

const tempDirs: string[] = [];

function makeProject(): string {
	const dir = mkdtempSync(join(tmpdir(), "sf-cli-status-"));
	tempDirs.push(dir);
	return dir;
}

afterEach(() => {
	for (const dir of tempDirs) rmSync(dir, { recursive: true, force: true });
	tempDirs.length = 0;
});

function snapshot() {
	return {
		state: {
			activeMilestone: { id: "M001", title: "Milestone" },
			activeSlice: { id: "S01", title: "Slice" },
			activeTask: { id: "T01", title: "Task" },
			phase: "executing",
			nextAction: "execute",
		},
		next: {
			action: "dispatch",
			unitType: "execute-task",
			unitId: "M001/S01/T01",
		},
		cost: { total: 0, workers: [] },
	};
}

test("resolveRecoveryPick_auto_selects_most_recent_row", () => {
	const picked = resolveRecoveryPick(
		"/tmp",
		[
			{
				unitType: "execute-task",
				unitId: "M001/S01/T01",
				updatedAt: 100,
			},
			{
				unitType: "discuss-milestone",
				unitId: "M001-X",
				updatedAt: 200,
			},
		],
		undefined,
		() => null,
	);
	assert.deepEqual(picked, {
		unitType: "discuss-milestone",
		unitId: "M001-X",
	});
});

test("resolveRecoveryPick_explicit_unitId_uses_matching_unitType", () => {
	const picked = resolveRecoveryPick(
		"/tmp",
		[
			{
				unitType: "discuss-milestone",
				unitId: "M001-X",
				updatedAt: 500,
			},
			{
				unitType: "execute-task",
				unitId: "M001/S01/T01",
				updatedAt: 900,
			},
		],
		"M001-X",
		() => null,
	);
	assert.deepEqual(picked, {
		unitType: "discuss-milestone",
		unitId: "M001-X",
	});
});

test("resolveRecoveryPick_fallback_to_execute_task_when_not_in_list_but_on_disk", () => {
	const picked = resolveRecoveryPick(
		"/tmp",
		[],
		"M001/S01/T01",
		(_base, ut, uid) =>
			ut === "execute-task" && uid === "M001/S01/T01" ? { status: "x" } : null,
	);
	assert.deepEqual(picked, {
		unitType: "execute-task",
		unitId: "M001/S01/T01",
	});
});

test("runStatusCli_recovery_when_newest_runtime_is_non_execute_task_succeeds", async () => {
	const project = makeProject();
	const { writeUnitRuntimeRecord } = await import(
		"../resources/extensions/sf/uok/unit-runtime.js"
	);
	const tOld = Date.now() - 5_000;
	writeUnitRuntimeRecord(project, "execute-task", "M001/S01/T01", tOld, {
		status: "running",
	});
	writeUnitRuntimeRecord(project, "discuss-milestone", "M002-Y", Date.now(), {
		status: "failed",
	});
	let out = "";
	let err = "";
	const code = await runStatusCli(["status", "recovery"], {
		basePath: project,
		stdout: {
			write(s: string) {
				out += s;
			},
			isTTY: false,
		},
		stderr: {
			write(s: string) {
				err += s;
			},
		},
	});
	assert.equal(code, 0);
	assert.match(out, /discuss-milestone\s+M002-Y/);
	assert.match(out, /failed/);
	assert.equal(err, "");
});

test("renderLiveStatus_when_solver_state_exists_includes_solver_line", () => {
	const project = makeProject();
	const solverDir = join(project, ".sf/runtime/autonomous-solver");
	mkdirSync(solverDir, { recursive: true });
	writeFileSync(
		join(solverDir, "active.json"),
		`${JSON.stringify({
			unitType: "execute-task",
			unitId: "M001/S01/T01",
			iteration: 3,
			maxIterations: 12,
			latestCheckpoint: {
				outcome: "continue",
				summary: "Implementation remains.",
				remainingItems: ["Finish enforcement", "Run build"],
			},
		})}\n`,
	);

	const rendered = renderLiveStatus(snapshot() as any, {
		basePath: project,
		model: null,
		recentEvents: [],
	});

	assert.match(rendered, /Solver:/);
	assert.match(rendered, /execute-task M001\/S01\/T01/);
	assert.match(rendered, /iter 3\/12/);
	assert.match(rendered, /2 remaining/);
});
