import { createHash } from "node:crypto";
import { spawnSync } from "node:child_process";
import { existsSync, mkdirSync, readFileSync, 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-07-${phase}-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const cases = [
  { id: "TDD-WP0-SEC-001-credential-channel", evidence: ["process-env-scan.json", "pipe-acl.json", "redaction.json"] },
  { id: "TDD-WP0-SUP-001-lifecycle", evidence: ["process-tree.json", "port.json", "supervisor-events.json", "screenshots/system-ui.png"] },
];
if (existsSync(runDirectory)) throw new Error(`Evidence run already exists: ${runId}`);
for (const testCase of cases) mkdirSync(resolve(runDirectory, "cases", testCase.id), { recursive: true });

const commandsToRun = phase === "red"
  ? [["dotnet run --project supervisor/Dada.Supervisor.Tests/Dada.Supervisor.Tests.csproj", ["run", "--project", "supervisor/Dada.Supervisor.Tests/Dada.Supervisor.Tests.csproj"]]]
  : [
      ["pnpm test:security", ["test:security"]],
      ["pnpm test:package", ["test:package"]],
      ["pnpm validate:tdd-trace", ["validate:tdd-trace"]],
    ];
const environment = {
  ...process.env,
  DADA_EVIDENCE_DIR_SEC: resolve(runDirectory, "cases", cases[0].id),
  DADA_EVIDENCE_DIR_SUP: resolve(runDirectory, "cases", cases[1].id),
};
const startedAt = new Date().toISOString();
const commands = [];
for (const [command, args] of commandsToRun) {
  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", `${phase === "red" ? "dotnet" : "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 });
}

const commit = spawnSync("git", ["rev-parse", "--short", "HEAD"], { encoding: "utf8" }).stdout.trim();
const manifest = { path: "tasks.manifest.json", sha256: createHash("sha256").update(readFileSync("tasks.manifest.json")).digest("hex").toUpperCase() };
const results = [];
for (const testCase of cases) {
  const caseDirectory = resolve(runDirectory, "cases", testCase.id);
  writeFileSync(resolve(caseDirectory, "commands.json"), `${JSON.stringify({ commands, phase, run_id: runId, schema_version: "1.0" }, null, 2)}\n`);
  const missingEvidence = phase === "green" ? testCase.evidence.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: testCase.id.includes("SEC") ? ["AC-41"] : ["AC-24", "AC-41"],
    automation: ["automated", "manual_review"],
    commit,
    environment: { arch: process.arch, node: process.version.slice(1), os: process.platform },
    evidence_refs: testCase.evidence,
    finished_at: new Date().toISOString(),
    layer: testCase.id.includes("SEC") ? ["SECURITY", "PACKAGE"] : ["PACKAGE", "SYSTEM_UI"],
    manifest,
    missing_evidence: missingEvidence,
    parent_family: testCase.id.replace(/-credential-channel|-lifecycle/, ""),
    phase,
    release_gate: ["work_package:WP-0", "release:P0-A"],
    requirements: testCase.id.includes("SEC") ? ["PRIV-01", "NFR-09"] : ["NFR-09"],
    run_id: runId,
    schema_version: "1.0",
    started_at: startedAt,
    status,
    task_id: "TASK-WP0-07",
    test_id: testCase.id,
    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`);
  results.push({ missing_evidence: missingEvidence, status, test_id: testCase.id });
}
const expectedStatus = phase === "red" ? "red_confirmed" : "passed";
const status = results.every(result => result.status === expectedStatus) ? expectedStatus : "failed";
const summary = { cases: results, 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 !== expectedStatus) process.exit(1);
