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 ?? `wp1-06-${phase}-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const testId = "TDD-WP1-AUD-001-immutable-triggers";
const runDirectory = resolve("artifacts", "tdd", runId);
const caseDirectory = resolve(runDirectory, "cases", testId);
if (existsSync(runDirectory)) throw new Error(`Evidence run already exists: ${runId}`);
mkdirSync(caseDirectory, { recursive: true });

const commandsToRun = phase === "red"
  ? [["audit-integration", ["exec", "vitest", "run", "tests/integration/wp1-06-audit-immutability.test.ts"]]]
  : [["integration", ["test:integration"]], ["tdd-trace", ["validate:tdd-trace"]]];
const commandResults = [];
for (const [name, args] of commandsToRun) {
  const command = `pnpm ${args.join(" ")}`;
  const started_at = new Date().toISOString();
  const execution = spawnSync(process.env.ComSpec ?? "cmd.exe", ["/d", "/s", "/c", command], {
    encoding: "utf8",
    env: { ...process.env, DADA_EVIDENCE_DIR_AUDIT: caseDirectory },
  });
  if (execution.stdout) process.stdout.write(execution.stdout);
  if (execution.stderr) process.stderr.write(execution.stderr);
  commandResults.push({ command, exit_code: execution.status ?? 1, finished_at: new Date().toISOString(), name, started_at });
}
writeFileSync(resolve(caseDirectory, "commands.json"), `${JSON.stringify({ commands: commandResults, phase, run_id: runId }, null, 2)}\n`);
const expectedEvidence = phase === "red"
  ? ["red-observation.json"]
  : ["sql-results.json", "db-diff.json", "trigger-definition.json"];
const commandState = phase === "red"
  ? commandResults.every((result) => result.exit_code !== 0)
  : commandResults.every((result) => result.exit_code === 0);
if (phase === "red") {
  writeFileSync(resolve(caseDirectory, "red-observation.json"), `${JSON.stringify({
    expected_failure: "audit retention capability, exact expiry and summary constraints are incomplete",
    status: commandState ? "red_confirmed" : "failed",
  }, null, 2)}\n`);
}
const evidence_refs = expectedEvidence;
const missing_evidence = evidence_refs.filter((file) => !existsSync(resolve(caseDirectory, file)));
const targetStatus = phase === "red" ? "red_confirmed" : "passed";
const status = commandState && missing_evidence.length === 0 ? targetStatus : "failed";
const result = {
  acceptance_criteria: ["AC-50"], automation: ["automated"],
  commit: spawnSync("git", ["rev-parse", "--short", "HEAD"], { encoding: "utf8" }).stdout.trim(),
  evidence_refs, layer: ["DB"],
  manifest: { path: "tasks.manifest.json", sha256: createHash("sha256").update(readFileSync("tasks.manifest.json")).digest("hex").toUpperCase() },
  missing_evidence, phase, requirements: ["ADMIN-05", "ADMIN-09"], run_id: runId, status,
  task_id: "TASK-WP1-06", test_id: testId, work_package: "WP-1",
  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, status, test_id: testId }], 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 !== targetStatus) process.exit(1);
