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-08-${phase}-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const caseId = "TDD-WP0-LOG-001-rotation-redaction";
const caseDirectory = resolve(runDirectory, "cases", caseId);
const uiDirectory = resolve(runDirectory, "ui", "system-ui", "unavailable");
if (existsSync(runDirectory)) throw new Error(`Evidence run already exists: ${runId}`);
mkdirSync(caseDirectory, { recursive: true });
mkdirSync(uiDirectory, { recursive: true });

const commandSpecs = phase === "red"
  ? [
      ["pnpm exec vitest run tests/integration/wp0-08-structured-log.test.ts", ["exec", "vitest", "run", "tests/integration/wp0-08-structured-log.test.ts"]],
      ["pnpm exec vitest run tests/worker/wp0-08-log-availability.test.ts", ["exec", "vitest", "run", "tests/worker/wp0-08-log-availability.test.ts"]],
    ]
  : [
      ["pnpm test:integration", ["test:integration"]],
      ["pnpm test:worker", ["test:worker"]],
      ["pnpm test:security", ["test:security"]],
      ["pnpm test:package", ["test:package"]],
      ["pnpm validate:tdd-trace", ["validate:tdd-trace"]],
    ];
const environment = { ...process.env, DADA_EVIDENCE_DIR_LOG: caseDirectory, DADA_EVIDENCE_DIR_SUP: uiDirectory };
const startedAt = new Date().toISOString();
const commands = [];
for (const [command, args] of commandSpecs) {
  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 });
}

writeFileSync(resolve(caseDirectory, "commands.json"), `${JSON.stringify({ commands, phase, run_id: runId, schema_version: "1.0" }, null, 2)}\n`);
const evidenceRefs = ["log-manifest.json", "redaction.json", "response.json", "external-calls.json"];
const missingEvidence = phase === "green" ? evidenceRefs.filter((path) => !existsSync(resolve(caseDirectory, path))) : [];
const uiEvidenceRefs = ["ui/system-ui/unavailable/screenshots/storage-unavailable.png"];
const missingUiEvidence = phase === "green" ? uiEvidenceRefs.filter((path) => !existsSync(resolve(runDirectory, 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 && missingUiEvidence.length === 0 ? "passed" : "failed");
const result = {
  acceptance_criteria: ["AC-41", "AC-56"],
  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: evidenceRefs,
  finished_at: new Date().toISOString(),
  layer: ["DB", "WORKER", "PACKAGE_SECURITY"],
  manifest: { path: "tasks.manifest.json", sha256: createHash("sha256").update(readFileSync("tasks.manifest.json")).digest("hex").toUpperCase() },
  missing_evidence: missingEvidence,
  missing_ui_evidence: missingUiEvidence,
  parent_family: "TDD-WP0-LOG-001",
  phase,
  release_gate: ["work_package:WP-0", "release:P0-A"],
  requirements: ["NFR-09", "PRIV-01", "PRIV-02"],
  run_id: runId,
  schema_version: "1.0",
  started_at: startedAt,
  status,
  task_id: "TASK-WP0-08",
  test_id: caseId,
  ui_evidence_refs: uiEvidenceRefs,
  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);
