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 ?? `wp0-05-${phase}-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const cases = [
  "TDD-WP0-FILE-001-atomic-commit",
  "TDD-WP0-STO-001-byte-thresholds",
  "TDD-WP0-STO-002-equal-limit",
  "TDD-WP0-STO-002-over-limit",
  "TDD-WP0-STO-003-cleanup-remeasure",
  "TDD-WP0-STO-003-full-matrix",
  "TDD-WP0-STO-003-unavailable-matrix",
];
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 redCommands = [
  ["pnpm exec vitest run tests/unit/wp0-05-storage-policy.test.ts", ["exec", "vitest", "run", "tests/unit/wp0-05-storage-policy.test.ts"]],
  ["pnpm exec vitest run tests/integration/wp0-05-managed-storage.test.ts", ["exec", "vitest", "run", "tests/integration/wp0-05-managed-storage.test.ts"]],
  ["pnpm exec vitest run tests/api/wp0-05-storage-capacity.test.ts", ["exec", "vitest", "run", "tests/api/wp0-05-storage-capacity.test.ts"]],
  ["pnpm exec vitest run tests/worker/wp0-05-storage-maintenance.test.ts", ["exec", "vitest", "run", "tests/worker/wp0-05-storage-maintenance.test.ts"]],
  ["pnpm exec playwright test tests/e2e/storage-capacity.spec.ts --config playwright.config.ts", ["exec", "playwright", "test", "tests/e2e/storage-capacity.spec.ts", "--config", "playwright.config.ts"]],
];
const greenCommands = [
  ["pnpm test:integration", ["test:integration"]],
  ["pnpm test:api", ["test:api"]],
  ["pnpm test:worker", ["test:worker"]],
  ["pnpm test:unit", ["test:unit"]],
  ["pnpm test:e2e", ["test:e2e"]],
  ["pnpm validate:tdd-trace", ["validate:tdd-trace"]],
];
const env = {
  ...process.env,
  DADA_EVIDENCE_DIR_FILE_001: directories[cases[0]],
  DADA_EVIDENCE_DIR_STO_001: directories[cases[1]],
  DADA_EVIDENCE_DIR_STO_002_EQUAL: directories[cases[2]],
  DADA_EVIDENCE_DIR_STO_002_OVER: directories[cases[3]],
  DADA_EVIDENCE_DIR_STO_003_CLEANUP: directories[cases[4]],
  DADA_EVIDENCE_DIR_STO_003_FULL: directories[cases[5]],
  DADA_EVIDENCE_DIR_STO_003_UNAVAILABLE: directories[cases[6]],
  DADA_PLAYWRIGHT_OUTPUT_DIR: playwrightDirectory,
};
const startedAt = new Date().toISOString();
const commands = [];
for (const [command, args] of phase === "red" ? redCommands : greenCommands) {
  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 });
  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 });
}

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 trace = find(playwrightDirectory, "trace.zip").find((path) => path.replaceAll("\\", "/").includes("storage-capacity"));
  if (trace) {
    copyFileSync(trace, resolve(directories[cases[5]], "trace.zip"));
    copyFileSync(trace, resolve(directories[cases[6]], "trace.zip"));
  }
}

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 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 expectedEvidence = {
  [cases[0]]: ["response.json", "db-diff.json", "fs-before.json", "fs-after.json", "crash-points.json"],
  [cases[1]]: ["thresholds.json", "db-diff.json", "screenshots/capacity-bar.png"],
  [cases[2]]: ["db-diff.json"],
  [cases[3]]: [
    "reference-response.json", "reference-db-diff.json", "reference-fs-diff.json", "reference-external-calls.json",
    "generated-response.json", "generated-db-diff.json", "generated-fs-diff.json", "generated-external-calls.json",
    "export-response.json", "export-db-diff.json", "export-fs-diff.json", "export-external-calls.json",
    "sticker_original-response.json", "sticker_original-db-diff.json", "sticker_original-fs-diff.json", "sticker_original-external-calls.json",
    "sticker_thumbnail-response.json", "sticker_thumbnail-db-diff.json", "sticker_thumbnail-fs-diff.json", "sticker_thumbnail-external-calls.json",
  ],
  [cases[4]]: ["conflict-response.json", "queued-db-diff.json", "worker-result.json", "fs-diff.json"],
  [cases[5]]: ["response.json", "db-diff.json", "external-calls.json", "trace.zip"],
  [cases[6]]: ["matrix.json", "db-diff.json", "external-calls.json", "trace.zip", "screenshots/unavailable.png"],
};
const commandState = phase === "red" ? commands.every((item) => item.exit_code !== 0) : commands.every((item) => item.exit_code === 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-31", "AC-35", "AC-44", "AC-55"], automation: ["automated"], commit,
    environment: { arch: process.arch, node: process.version.slice(1), os: process.platform }, evidence_refs,
    finished_at: new Date().toISOString(), layer: ["DB", "API", "WORKER", "E2E"], manifest, missing_evidence,
    parent_family: testId.split("-").slice(0, -2).join("-"), phase, release_gate: ["work_package:WP-0", "release:P0-A"],
    requirements: ["ADMIN-06", "EXPORT-06", "NFR-05", "PROJECT-04", "17.19"], run_id: runId,
    schema_version: "1.0", started_at: startedAt, status, task_id: "TASK-WP0-05", test_id: testId,
    work_package: "WP-0", worktree_under_test: worktreeDirty ? "uncommitted implementation" : "clean committed implementation",
  };
  writeFileSync(resolve(directories[testId], "result.json"), `${JSON.stringify(result, null, 2)}\n`);
  return result;
});
const passed = results.every((result) => result.status === (phase === "red" ? "red_confirmed" : "passed"));
const summary = { cases: results.map(({ missing_evidence, status, test_id }) => ({ missing_evidence, status, test_id })), phase, run_id: runId, status: passed ? (phase === "red" ? "red_confirmed" : "passed") : "failed" };
writeFileSync(resolve(runDirectory, "evidence.json"), `${JSON.stringify(summary, null, 2)}\n`);
console.log(JSON.stringify(summary, null, 2));
if (!passed) process.exit(1);
