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-03-${phase}-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const noticeCase = "TDD-WP1-NOTICE-001-registration-consent";
const slotCase = "TDD-WP1-SLOT-001-stage-limit";
const directories = {
  [noticeCase]: resolve(runDirectory, "cases", noticeCase),
  [slotCase]: resolve(runDirectory, "cases", slotCase),
};
if (existsSync(runDirectory)) throw new Error(`Evidence run already exists: ${runId}`);
for (const directory of Object.values(directories)) mkdirSync(directory, { recursive: true });

const commandsToRun = phase === "red"
  ? [
      ["notice-unit", ["exec", "vitest", "run", "tests/unit/wp1-03-notice.test.ts"]],
      ["slot-integration", ["exec", "vitest", "run", "tests/integration/wp1-03-slot-limit.test.ts"]],
      ["notice-e2e", ["exec", "playwright", "test", "tests/e2e/user-registration.spec.ts", "--config", "playwright.config.ts"]],
    ]
  : [
      ["unit", ["test:unit"]],
      ["api", ["test:api"]],
      ["e2e", ["test:e2e"]],
      ["integration", ["test:integration"]],
      ["tdd-trace", ["validate:tdd-trace"]],
    ];
const environment = {
  ...process.env,
  DADA_EVIDENCE_DIR_NOTICE: directories[noticeCase],
  DADA_EVIDENCE_DIR_SLOT: directories[slotCase],
};
const commandResults = [];
for (const [name, args] of commandsToRun) {
  const command = `pnpm ${args.join(" ")}`;
  const executable = process.env.ComSpec ?? "cmd.exe";
  const started_at = new Date().toISOString();
  const execution = spawnSync(executable, ["/d", "/s", "/c", command], { encoding: "utf8", env: environment });
  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 });
}

const byName = Object.fromEntries(commandResults.map((result) => [result.name, result]));
const statuses = phase === "red"
  ? {
      [noticeCase]: byName["notice-unit"].exit_code !== 0 && byName["notice-e2e"].exit_code !== 0 ? "red_confirmed" : "failed",
      [slotCase]: byName["slot-integration"].exit_code === 0 ? "preexisting_green" : "red_confirmed",
    }
  : {
      [noticeCase]: commandResults.every((result) => result.exit_code === 0) ? "awaiting_manual_review" : "failed",
      [slotCase]: commandResults.every((result) => result.exit_code === 0) ? "passed" : "failed",
    };
for (const directory of Object.values(directories)) {
  writeFileSync(resolve(directory, "commands.json"), `${JSON.stringify({ commands: commandResults, phase, run_id: runId }, null, 2)}\n`);
}
if (phase === "red") {
  writeFileSync(resolve(directories[noticeCase], "red-observation.json"), `${JSON.stringify({
    expected_failures: ["registration notice contract missing", "DVPM8 second stage missing"],
    status: statuses[noticeCase],
  }, null, 2)}\n`);
  writeFileSync(resolve(directories[slotCase], "red-observation.json"), `${JSON.stringify({
    explanation: "TASK-WP1-01 already implemented the normative slot transaction; no failure was fabricated.",
    status: statuses[slotCase],
  }, null, 2)}\n`);
}
const expected = phase === "red"
  ? { [noticeCase]: ["red-observation.json"], [slotCase]: ["red-observation.json"] }
  : {
      [noticeCase]: ["response.json", "db-diff.json", "screenshots/notice-expanded.png", "manual-review.json"],
      [slotCase]: ["response.json", "db-diff.json", "concurrency-trace.json"],
    };
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 worktreeDirty = spawnSync("git", ["status", "--porcelain"], { encoding: "utf8" }).stdout.trim().length > 0;
const results = Object.entries(statuses).map(([testId, status]) => {
  const evidence_refs = expected[testId];
  const missing_evidence = evidence_refs.filter((file) => !existsSync(resolve(directories[testId], file)));
  const result = {
    acceptance_criteria: ["AC-01", "AC-32", "AC-41", "AC-45", "AC-56"],
    automation: ["automated", "manual_review"],
    commit,
    evidence_refs,
    manifest,
    missing_evidence,
    phase,
    requirements: ["AUTH-01", "AUTH-03", "NFR-04", "PRIV-04", "PRIV-06"],
    run_id: runId,
    status,
    task_id: "TASK-WP1-03",
    test_id: testId,
    work_package: "WP-1",
    worktree_under_test: worktreeDirty ? "uncommitted implementation" : "clean committed implementation",
  };
  writeFileSync(resolve(directories[testId], "result.json"), `${JSON.stringify(result, null, 2)}\n`);
  return result;
});
const redAccepted = phase === "red" && statuses[noticeCase] === "red_confirmed" && ["preexisting_green", "red_confirmed"].includes(statuses[slotCase]);
const greenAccepted = phase === "green" && statuses[noticeCase] === "awaiting_manual_review" && statuses[slotCase] === "passed";
const summary = { cases: results.map(({ missing_evidence, status, test_id }) => ({ missing_evidence, status, test_id })), phase, run_id: runId, status: redAccepted ? "red_confirmed" : greenAccepted ? "awaiting_manual_review" : "failed" };
writeFileSync(resolve(runDirectory, "evidence.json"), `${JSON.stringify(summary, null, 2)}\n`);
console.log(JSON.stringify(summary, null, 2));
if (!redAccepted && !greenAccepted) process.exit(1);
