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

const errorCases = [
  "upstream-timeout", "upstream-failed", "safety-rejected", "model-disabled", "gateway-balance",
  "gateway-contract", "reference-invalid", "unknown-retryable", "unknown-non-retryable",
].map((suffix) => ({
  acceptance: ["AC-04", "AC-53"],
  evidence: [
    "response.json",
    "db-diff.json",
    ...(suffix === "model-disabled" || suffix.startsWith("gateway-") ? ["external-calls.json"] : ["worker-events.json"]),
    `screenshots/${suffix === "gateway-balance" ? "gateway_balance_insufficient" : suffix === "gateway-contract" ? "gateway_contract_invalid" : suffix.replaceAll("-", "_")}.png`,
  ],
  id: `TDD-WP2-ERR-001-${suffix}`,
  requirements: ["GEN-16", "17.18"],
}));
const cases = [
  { acceptance: ["AC-04", "AC-51"], evidence: ["response.json", "db-diff.json", "worker-events.json", "external-calls.json"], id: "TDD-WP2-BAL-001-balance-impact", requirements: ["GEN-14", "GEN-15", "17.17"] },
  ...errorCases,
  { acceptance: ["AC-03"], evidence: ["worker-events.json", "db-diff.json", "fs-after.json", "screenshots/succeeded.png"], id: "TDD-WP2-GEN-002-success", requirements: ["CREDIT-03", "GEN-06", "GEN-08", "GEN-11"] },
];
for (const item of cases) mkdirSync(resolve(casesDirectory, item.id), { recursive: true });

const commands = phase === "red"
  ? [
      ["unit", ["exec", "vitest", "run", "tests/unit/wp2-06-generation-error-registry.test.ts"]],
      ["integration", ["exec", "vitest", "run", "tests/integration/wp2-06-gateway-balance.test.ts"]],
      ["worker", ["exec", "vitest", "run", "tests/worker/wp2-06-generation-processor.test.ts"]],
      ["e2e", ["exec", "playwright", "test", "tests/e2e/generation-terminal-actions.spec.ts", "--config", "playwright.config.ts"]],
    ]
  : [["unit", ["test:unit"]], ["integration", ["test:integration"]], ["api", ["test:api"]], ["worker", ["test:worker"]], ["e2e", ["test:e2e"]], ["tdd-trace", ["validate:tdd-trace"]]];
const environment = { ...process.env, DADA_EVIDENCE_DIR_GENERATION_RUNTIME: casesDirectory };
const commandResults = [];
for (const [name, args] of commands) {
  const command = `pnpm ${args.join(" ")}`;
  const started_at = new Date().toISOString();
  const result = spawnSync(process.env.ComSpec ?? "cmd.exe", ["/d", "/s", "/c", command], { encoding: "utf8", env: environment });
  if (result.stdout) process.stdout.write(result.stdout);
  if (result.stderr) process.stderr.write(result.stderr);
  commandResults.push({ command, exit_code: result.status ?? 1, finished_at: new Date().toISOString(), name, started_at });
}
const commandState = phase === "red" ? commandResults.every((result) => result.exit_code !== 0) : commandResults.every((result) => result.exit_code === 0);
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 dirty = spawnSync("git", ["status", "--porcelain"], { encoding: "utf8" }).stdout.trim().length > 0;
const summaries = [];
for (const item of cases) {
  const directory = resolve(casesDirectory, item.id);
  writeFileSync(resolve(directory, "commands.json"), `${JSON.stringify({ commands: commandResults, phase, run_id: runId }, null, 2)}\n`);
  if (phase === "red") writeFileSync(resolve(directory, "red-observation.json"), `${JSON.stringify({ expected_failure: "Generation terminal processor, fixed error registry, gateway balance scope and terminal UI actions are absent", status: commandState ? "red_confirmed" : "failed" }, null, 2)}\n`);
  const evidenceRefs = phase === "red" ? ["red-observation.json"] : item.evidence;
  const missingEvidence = evidenceRefs.filter((file) => !existsSync(resolve(directory, file)));
  const targetStatus = phase === "red" ? "red_confirmed" : "passed";
  const status = commandState && missingEvidence.length === 0 ? targetStatus : "failed";
  writeFileSync(resolve(directory, "result.json"), `${JSON.stringify({
    acceptance_criteria: item.acceptance, automation: ["automated"], commit, evidence_refs: evidenceRefs,
    layer: ["DB", "API", "WRK", "UNIT", "E2E"], manifest, missing_evidence: missingEvidence, phase,
    requirements: item.requirements, run_id: runId, status, task_id: "TASK-WP2-06", test_id: item.id,
    work_package: "WP-2", worktree_under_test: dirty ? "uncommitted implementation" : "clean committed implementation",
  }, null, 2)}\n`);
  summaries.push({ missing_evidence: missingEvidence, status, test_id: item.id });
}
const targetStatus = phase === "red" ? "red_confirmed" : "passed";
const status = summaries.every((item) => item.status === targetStatus) ? targetStatus : "failed";
writeFileSync(resolve(runDirectory, "evidence.json"), `${JSON.stringify({ cases: summaries, phase, run_id: runId, status }, null, 2)}\n`);
console.log(JSON.stringify({ cases: summaries, phase, run_id: runId, status }, null, 2));
if (status !== targetStatus) process.exit(1);
