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-01-${phase}-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const cases = [
  "TDD-WP1-AUTH-001-valid-register",
  "TDD-WP1-AUTH-002-invalid-invite",
  "TDD-WP1-AUTH-001-final-recheck-race",
];
const directories = Object.fromEntries(cases.map((id) => [id, resolve(runDirectory, "cases", id)]));
if (existsSync(runDirectory)) throw new Error(`Evidence run already exists: ${runId}`);
for (const directory of Object.values(directories)) mkdirSync(directory, { recursive: true });

const commandSpecs = phase === "red"
  ? [
      ["pnpm exec vitest run tests/integration/wp1-01-registration.test.ts", ["exec", "vitest", "run", "tests/integration/wp1-01-registration.test.ts"]],
      ["pnpm exec vitest run tests/api/wp1-01-registration.test.ts", ["exec", "vitest", "run", "tests/api/wp1-01-registration.test.ts"]],
    ]
  : [
      ["pnpm test:integration", ["test:integration"]],
      ["pnpm test:api", ["test:api"]],
      ["pnpm test:e2e", ["test:e2e"]],
      ["pnpm validate:tdd-trace", ["validate:tdd-trace"]],
    ];
const environment = {
  ...process.env,
  DADA_EVIDENCE_DIR_AUTH_INVALID: directories[cases[1]],
  DADA_EVIDENCE_DIR_AUTH_RACE: directories[cases[2]],
  DADA_EVIDENCE_DIR_AUTH_VALID: directories[cases[0]],
};
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 });
}

const commandEvidence = { commands, phase, run_id: runId, schema_version: "1.0" };
for (const directory of Object.values(directories)) {
  writeFileSync(resolve(directory, "commands.json"), `${JSON.stringify(commandEvidence, null, 2)}\n`);
}
const expectedEvidence = {
  [cases[0]]: ["request.json", "response.json", "db-diff.json"],
  [cases[1]]: [
    "expired-response.json", "disabled-response.json", "exhausted-response.json", "not_found-response.json",
    "db-diff.json", "external-calls.json",
  ],
  [cases[2]]: [
    "exhausted-response.json", "disabled-response.json", "expired-response.json", "stage_limit-response.json",
    "exhausted-db-diff.json", "disabled-db-diff.json", "expired-db-diff.json", "stage_limit-db-diff.json",
    "transaction-trace.json",
  ],
};
const manifest = {
  path: "tasks.manifest.json",
  sha256: createHash("sha256").update(readFileSync("tasks.manifest.json")).digest("hex").toUpperCase(),
};
const commandState = phase === "red" ? commands.every((item) => item.exit_code !== 0) : commands.every((item) => item.exit_code === 0);
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 = cases.map((testId) => {
  const evidence_refs = expectedEvidence[testId];
  const missing_evidence = phase === "green" ? evidence_refs.filter((path) => !existsSync(resolve(directories[testId], path))) : [];
  const status = phase === "red"
    ? (commandState ? "red_confirmed" : "failed")
    : (commandState && missing_evidence.length === 0 ? "passed" : "failed");
  const result = {
    acceptance_criteria: ["AC-01", "AC-02", "AC-45"],
    automation: ["automated"],
    commit,
    deferred_e2e_evidence: {
      reason: "Registration UI ownership begins in TASK-WP1-03; no UI or screenshot is fabricated by TASK-WP1-01.",
      required_by_case: ["trace.zip", "screenshots/"],
      task_id: "TASK-WP1-03",
    },
    environment: { arch: process.arch, node: process.version.slice(1), os: process.platform },
    evidence_refs,
    finished_at: new Date().toISOString(),
    layer: ["DB", "API", "E2E"],
    manifest,
    missing_evidence,
    parent_family: testId.match(/^(TDD-WP[0-7]-[A-Z0-9]+-[0-9]{3})-/)?.[1],
    phase,
    release_gate: ["work_package:WP-1", "release:P0-A"],
    requirements: ["AUTH-01", "AUTH-02", "AUTH-03", "AUTH-04", "NFR-04"],
    run_id: runId,
    schema_version: "1.0",
    started_at: startedAt,
    status,
    task_id: "TASK-WP1-01",
    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 targetStatus = phase === "red" ? "red_confirmed" : "passed";
const passed = results.every((result) => result.status === targetStatus);
const summary = {
  cases: results.map(({ missing_evidence, status, test_id }) => ({ missing_evidence, status, test_id })),
  phase,
  run_id: runId,
  status: passed ? targetStatus : "failed",
};
writeFileSync(resolve(runDirectory, "evidence.json"), `${JSON.stringify(summary, null, 2)}\n`);
console.log(JSON.stringify(summary, null, 2));
if (!passed) process.exit(1);
