import { createHash } from "node:crypto";
import { spawnSync } from "node:child_process";
import { copyFileSync, existsSync, mkdirSync, readFileSync, readdirSync, statSync, 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-02-${phase}-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const cases = [
  "TDD-WP1-AUTH-003-entry-state-matrix",
  "TDD-WP1-AUTH-004-challenge-guards",
  "TDD-WP1-AUTH-004-session-revocation",
];
const directories = Object.fromEntries(cases.map((id) => [id, resolve(runDirectory, "cases", id)]));
const playwrightDirectory = resolve(runDirectory, "playwright");
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"
  ? [
      ["pnpm exec vitest run tests/integration/wp1-02-auth-state.test.ts", ["exec", "vitest", "run", "tests/integration/wp1-02-auth-state.test.ts"]],
      ["pnpm exec vitest run tests/api/wp1-02-login-session.test.ts", ["exec", "vitest", "run", "tests/api/wp1-02-login-session.test.ts"]],
      ["pnpm exec playwright test tests/e2e/user-auth.spec.ts tests/e2e/entry-state-ui.spec.ts tests/e2e/session-invalid-ui.spec.ts --config playwright.config.ts", ["exec", "playwright", "test", "tests/e2e/user-auth.spec.ts", "tests/e2e/entry-state-ui.spec.ts", "tests/e2e/session-invalid-ui.spec.ts", "--config", "playwright.config.ts"]],
    ]
  : [
      ["pnpm test:api", ["test:api"]],
      ["pnpm test:e2e", ["test:e2e"]],
      ["pnpm test:integration", ["test:integration"]],
      ["pnpm validate:tdd-trace", ["validate:tdd-trace"]],
    ];
const environment = {
  ...process.env,
  DADA_EVIDENCE_DIR_AUTH_GUARDS: directories[cases[1]],
  DADA_EVIDENCE_DIR_AUTH_MATRIX: directories[cases[0]],
  DADA_EVIDENCE_DIR_AUTH_REVOKE: directories[cases[2]],
  DADA_PLAYWRIGHT_OUTPUT_DIR: playwrightDirectory,
};
const startedAt = new Date().toISOString();
const commandResults = [];
for (const [command, args] of commandsToRun) {
  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);
  commandResults.push({ command, exit_code: execution.status ?? 1, finished_at: new Date().toISOString(), started_at });
}

function find(root, name) {
  if (!existsSync(root)) return [];
  return readdirSync(root).flatMap((entry) => {
    const child = resolve(root, entry);
    return statSync(child).isDirectory() ? find(child, name) : entry === name ? [child] : [];
  });
}
if (phase === "green") {
  const traces = find(playwrightDirectory, "trace.zip");
  const entryTrace = traces.find((path) => path.replaceAll("\\", "/").includes("entry-state-ui"));
  const revocationTrace = traces.find((path) => path.replaceAll("\\", "/").includes("session-invalid-ui"));
  if (entryTrace) copyFileSync(entryTrace, resolve(directories[cases[0]], "trace.zip"));
  if (revocationTrace) copyFileSync(revocationTrace, resolve(directories[cases[2]], "trace.zip"));
}

const commands = { commands: commandResults, phase, run_id: runId, schema_version: "1.0" };
for (const directory of Object.values(directories)) writeFileSync(resolve(directory, "commands.json"), `${JSON.stringify(commands, null, 2)}\n`);
const expectedEvidence = {
  [cases[0]]: ["response.json", "db-diff.json", "trace.zip", "screenshots/entry-state.png"],
  [cases[1]]: ["response.json", "db-diff.json", "external-calls.json"],
  [cases[2]]: ["response.json", "db-diff.json", "trace.zip", "screenshots/revoked.png"],
};
const manifest = { path: "tasks.manifest.json", sha256: createHash("sha256").update(readFileSync("tasks.manifest.json")).digest("hex").toUpperCase() };
const commandState = phase === "red" ? commandResults.every((item) => item.exit_code !== 0) : commandResults.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-22", "AC-33", "AC-49"], automation: ["automated"], commit,
    environment: { arch: process.arch, node: process.version.slice(1), os: process.platform }, evidence_refs,
    finished_at: new Date().toISOString(), layer: ["API", "E2E", "DB"], 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-04", "AUTH-05", "AUTH-06", "AUTH-07"],
    run_id: runId, schema_version: "1.0", started_at: startedAt, status,
    task_id: "TASK-WP1-02", 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);
