import { createHash } from "node:crypto";
import { spawnSync } from "node:child_process";
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
import { resolve } from "node:path";

const phaseIndex = process.argv.indexOf("--phase");
const phase = phaseIndex >= 0 ? process.argv[phaseIndex + 1] : "green";
if (!["red", "green"].includes(phase)) throw new Error(`Unsupported phase: ${phase}`);
const runId = process.env.DADA_TDD_RUN_ID ?? `wp4-06-${phase}-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const caseDirectory = resolve(runDirectory, "cases", "TDD-WP4-A11Y-001-keyboard-focus");
const pageDirectory = resolve(caseDirectory, "pages", "editor");
if (existsSync(runDirectory)) throw new Error(`Evidence run already exists: ${runId}`);
mkdirSync(pageDirectory, { recursive: true });

const outputDirectory = resolve(runDirectory, "playwright-output");
const environment = { ...process.env, DADA_EVIDENCE_DIR_A11Y: caseDirectory, DADA_PLAYWRIGHT_OUTPUT_DIR: outputDirectory };
const commands = phase === "red" ? [] : [
  ["e2e", "pnpm test:e2e"],
  ["visual", "pnpm test:visual"],
  ["performance", "pnpm test:performance"],
  ["tdd-trace", "pnpm validate:tdd-trace"],
];
const commandResults = [];
for (const [name, command] of commands) {
  const started_at = new Date().toISOString();
  const result = spawnSync(process.env.ComSpec ?? "cmd.exe", ["/d", "/s", "/c", command], { encoding: "utf8", env: environment });
  if (result.stdout) process.stdout.write(result.stdout);
  if (result.stderr) process.stderr.write(result.stderr);
  commandResults.push({ command, exit_code: result.status ?? 1, finished_at: new Date().toISOString(), name, started_at });
}

function traces(directory) {
  if (!existsSync(directory)) return [];
  return readdirSync(directory, { withFileTypes: true }).flatMap((entry) => {
    const path = resolve(directory, entry.name);
    return entry.isDirectory() ? traces(path) : entry.name === "trace.zip" ? [path] : [];
  });
}
if (phase === "green") {
  const trace = traces(outputDirectory).find((path) => path.includes("wp4-06-accessibility"));
  if (trace) copyFileSync(trace, resolve(pageDirectory, "trace.zip"));
} else {
  writeFileSync(resolve(caseDirectory, "red-observation.json"), `${JSON.stringify({
    expected_failure: "Editor had no first-tab skip link, modal focus trap/return, keyboard all-object selection, or reachable export at 200 percent equivalent viewport",
    observed_command: "pnpm playwright test tests/e2e/wp4-06-accessibility.spec.ts --config playwright.config.ts",
    observed_error: "getByRole('link', { name: '跳到主要内容' }) did not exist",
    status: "red_confirmed",
  }, null, 2)}\n`);
}

const evidenceRefs = phase === "red" ? ["red-observation.json"] : [
  "pages/editor/axe.json", "pages/editor/focus-trace.json", "pages/editor/trace.zip", "pages/editor/screenshots/200pct.png",
];
const missing = evidenceRefs.filter((file) => !existsSync(resolve(caseDirectory, file)));
const commandState = phase === "red" || commandResults.every((result) => result.exit_code === 0);
const status = commandState && missing.length === 0 ? phase === "red" ? "red_confirmed" : "passed" : "failed";
const manifest = { path: "tasks.manifest.json", sha256: createHash("sha256").update(readFileSync("tasks.manifest.json")).digest("hex").toUpperCase() };
const commit = spawnSync("git", ["rev-parse", "--short", "HEAD"], { encoding: "utf8" }).stdout.trim();
const dirty = spawnSync("git", ["status", "--porcelain"], { encoding: "utf8" }).stdout.trim().length > 0;
writeFileSync(resolve(caseDirectory, "commands.json"), `${JSON.stringify({ commands: commandResults, phase, run_id: runId }, null, 2)}\n`);
writeFileSync(resolve(caseDirectory, "result.json"), `${JSON.stringify({
  acceptance_criteria: ["AC-32"], automation: ["automated"], commit, evidence_refs: evidenceRefs,
  layer: ["E2E", "VIS-PERF"], manifest, missing_evidence: missing, phase, requirements: ["NFR-06"],
  run_id: runId, status, task_id: "TASK-WP4-06", test_id: "TDD-WP4-A11Y-001-keyboard-focus", work_package: "WP-4",
  worktree_under_test: dirty ? "uncommitted implementation" : "clean committed implementation",
}, null, 2)}\n`);
writeFileSync(resolve(runDirectory, "commands.json"), `${JSON.stringify({ commands: commandResults, phase, run_id: runId }, null, 2)}\n`);
writeFileSync(resolve(runDirectory, "evidence.json"), `${JSON.stringify({ cases: [{ missing_evidence: missing, status, test_id: "TDD-WP4-A11Y-001-keyboard-focus" }], phase, run_id: runId, status }, null, 2)}\n`);
console.log(JSON.stringify({ cases: [{ missing_evidence: missing, status, test_id: "TDD-WP4-A11Y-001-keyboard-focus" }], phase, run_id: runId, status }, null, 2));
if (status === "failed") process.exit(1);
