import { createHash } from "node:crypto";
import { spawnSync } from "node:child_process";
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, statSync, 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 ?? `wp0-06-${phase}-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const caseId = "TDD-WP0-CACHE-001-public-lru";
const caseDirectory = resolve(runDirectory, "cases", caseId);
const playwrightDirectory = resolve(runDirectory, "playwright");
if (existsSync(runDirectory)) throw new Error(`Evidence run already exists: ${runId}`);
mkdirSync(caseDirectory, { recursive: true });

const redCommands = [
  ["pnpm exec vitest run tests/unit/wp0-06-public-cache.test.ts", ["exec", "vitest", "run", "tests/unit/wp0-06-public-cache.test.ts"]],
  ["pnpm exec playwright test tests/e2e/public-asset-cache.spec.ts --config playwright.config.ts", ["exec", "playwright", "test", "tests/e2e/public-asset-cache.spec.ts", "--config", "playwright.config.ts"]],
];
const greenCommands = [
  ["pnpm test:unit", ["test:unit"]],
  ["pnpm test:e2e", ["test:e2e"]],
  ["pnpm validate:tdd-trace", ["validate:tdd-trace"]],
];
const environment = {
  ...process.env,
  DADA_EVIDENCE_DIR_CACHE: caseDirectory,
  DADA_PLAYWRIGHT_OUTPUT_DIR: playwrightDirectory,
};
const startedAt = new Date().toISOString();
const commands = [];
for (const [command, args] of phase === "red" ? redCommands : greenCommands) {
  const started_at = new Date().toISOString();
  const executable = process.platform === "win32" ? (process.env.ComSpec ?? "cmd.exe") : "pnpm";
  const actualArgs = process.platform === "win32" ? ["/d", "/s", "/c", `pnpm ${args.join(" ")}`] : args;
  const execution = spawnSync(executable, actualArgs, { encoding: "utf8", env: environment });
  if (execution.stdout) process.stdout.write(execution.stdout);
  if (execution.stderr) process.stderr.write(execution.stderr);
  commands.push({ command, exit_code: execution.status ?? 1, finished_at: new Date().toISOString(), started_at });
}

function find(root, target) {
  if (!existsSync(root)) return [];
  return readdirSync(root).flatMap((entry) => {
    const child = resolve(root, entry);
    return statSync(child).isDirectory() ? find(child, target) : entry === target ? [child] : [];
  });
}
if (phase === "green") {
  const trace = find(playwrightDirectory, "trace.zip").find((path) => path.replaceAll("\\", "/").includes("public-asset-cache"));
  if (trace) copyFileSync(trace, resolve(caseDirectory, "trace.zip"));
}

writeFileSync(resolve(caseDirectory, "commands.json"), `${JSON.stringify({ commands, phase, run_id: runId, schema_version: "1.0" }, null, 2)}\n`);
const expectedEvidence = ["cache-enumeration.json", "lru-trace.json", "trace.zip"];
const missingEvidence = phase === "green" ? expectedEvidence.filter((path) => !existsSync(resolve(caseDirectory, path))) : [];
const commandState = phase === "red" ? commands.every((item) => item.exit_code !== 0) : commands.every((item) => item.exit_code === 0);
const status = phase === "red" ? (commandState ? "red_confirmed" : "failed") : (commandState && missingEvidence.length === 0 ? "passed" : "failed");
const result = {
  acceptance_criteria: ["AC-46", "AC-48"],
  automation: ["automated"],
  commit: spawnSync("git", ["rev-parse", "--short", "HEAD"], { encoding: "utf8" }).stdout.trim(),
  environment: { arch: process.arch, node: process.version.slice(1), os: process.platform },
  evidence_refs: expectedEvidence,
  finished_at: new Date().toISOString(),
  layer: ["UNIT", "E2E"],
  manifest: { path: "tasks.manifest.json", sha256: createHash("sha256").update(readFileSync("tasks.manifest.json")).digest("hex").toUpperCase() },
  missing_evidence: missingEvidence,
  parent_family: "TDD-WP0-CACHE-001",
  phase,
  release_gate: ["work_package:WP-0", "release:P0-A"],
  requirements: ["NFR-07", "17.14"],
  run_id: runId,
  schema_version: "1.0",
  started_at: startedAt,
  status,
  task_id: "TASK-WP0-06",
  test_id: caseId,
  work_package: "WP-0",
  worktree_under_test: spawnSync("git", ["status", "--porcelain"], { encoding: "utf8" }).stdout.trim() ? "uncommitted implementation" : "clean committed implementation",
};
writeFileSync(resolve(caseDirectory, "result.json"), `${JSON.stringify(result, null, 2)}\n`);
const summary = { cases: [{ missing_evidence: missingEvidence, status, test_id: caseId }], phase, run_id: runId, status };
writeFileSync(resolve(runDirectory, "evidence.json"), `${JSON.stringify(summary, null, 2)}\n`);
console.log(JSON.stringify(summary, null, 2));
if (status !== (phase === "red" ? "red_confirmed" : "passed")) process.exit(1);
