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 runId = process.env.DADA_TDD_RUN_ID ?? `wp0-02-green-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const apiCaseId = "TDD-WP0-API-001-schema-envelope";
const eventCaseId = "TDD-WP0-EVT-001-rest-refetch";
const apiDirectory = resolve(runDirectory, "cases", apiCaseId);
const eventDirectory = resolve(runDirectory, "cases", eventCaseId);
const playwrightDirectory = resolve(eventDirectory, "playwright");

if (existsSync(runDirectory)) throw new Error(`Evidence run already exists: ${runId}`);
mkdirSync(apiDirectory, { recursive: true });
mkdirSync(eventDirectory, { recursive: true });

const commandDefinitions = [
  { command: "pnpm test:unit", args: ["test:unit"] },
  { command: "pnpm test:api", args: ["test:api"] },
  { command: "pnpm test:e2e", args: ["test:e2e"] },
  { command: "pnpm validate:tdd-trace", args: ["validate:tdd-trace"] },
];
const startedAt = new Date().toISOString();
const commands = [];

for (const definition of commandDefinitions) {
  const commandStartedAt = new Date().toISOString();
  const executable = process.platform === "win32" ? (process.env.ComSpec ?? "cmd.exe") : "pnpm";
  const args = process.platform === "win32"
    ? ["/d", "/s", "/c", `pnpm ${definition.args.join(" ")}`]
    : definition.args;
  const execution = spawnSync(executable, args, {
    env: {
      ...process.env,
      DADA_EVIDENCE_DIR_API: apiDirectory,
      DADA_EVIDENCE_DIR_EVT: eventDirectory,
      DADA_PLAYWRIGHT_OUTPUT_DIR: playwrightDirectory,
    },
    stdio: "inherit",
  });
  commands.push({
    command: definition.command,
    exit_code: execution.status ?? 1,
    finished_at: new Date().toISOString(),
    started_at: commandStartedAt,
  });
}

function findFile(root, target) {
  if (!existsSync(root)) return undefined;
  for (const name of readdirSync(root)) {
    const child = resolve(root, name);
    if (statSync(child).isDirectory()) {
      const nested = findFile(child, target);
      if (nested) return nested;
    } else if (name === target) return child;
  }
  return undefined;
}

const trace = findFile(playwrightDirectory, "trace.zip");
if (trace) copyFileSync(trace, resolve(eventDirectory, "trace.zip"));

const commandEvidence = {
  commands,
  phase: "green",
  run_id: runId,
  schema_version: "1.0",
};
writeFileSync(resolve(apiDirectory, "commands.json"), `${JSON.stringify(commandEvidence, null, 2)}\n`);
writeFileSync(resolve(eventDirectory, "commands.json"), `${JSON.stringify(commandEvidence, null, 2)}\n`);

const manifestBytes = readFileSync("tasks.manifest.json");
const manifest = {
  path: "tasks.manifest.json",
  sha256: createHash("sha256").update(manifestBytes).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 environment = {
  arch: process.arch,
  node: process.version.slice(1),
  os: process.platform,
};

const cases = [
  {
    acceptance_criteria: [],
    directory: apiDirectory,
    evidence_refs: ["openapi.json", "snapshot-diff.json", "response.json", "redaction.json"],
    green_assertions: [
      "OpenAPI 3.1 snapshot and runtime schemas agree",
      "generated client matches the OpenAPI operations",
      "responses include a valid correlation ID",
      "error details accept only whitelisted non-sensitive fields",
    ],
    layer: ["UNIT", "API"],
    parent_family: "TDD-WP0-API-001",
    requirements: ["NFR-05", "DevelopmentPlan 6.1", "DevelopmentPlan 6.6"],
    test_id: apiCaseId,
  },
  {
    acceptance_criteria: ["AC-20", "AC-30"],
    directory: eventDirectory,
    evidence_refs: ["sse-events.json", "network-timeline.json", "trace.zip"],
    green_assertions: [
      "events contain only fixed non-sensitive fields",
      "each event causes a REST refetch",
      "disconnects and event ID gaps cause bootstrap recovery",
      "configuration and runtime availability versions remain separate",
    ],
    layer: ["API", "E2E"],
    parent_family: "TDD-WP0-EVT-001",
    requirements: ["PROJECT-04", "GEN-14", "NFR-05", "DevelopmentPlan 6.2"],
    test_id: eventCaseId,
  },
];

const commandsPassed = commands.every((command) => command.exit_code === 0);
const results = cases.map((testCase) => {
  const missingEvidence = testCase.evidence_refs.filter((file) => !existsSync(resolve(testCase.directory, file)));
  const passed = commandsPassed && missingEvidence.length === 0;
  const result = {
    acceptance_criteria: testCase.acceptance_criteria,
    automation: ["automated"],
    commit,
    environment,
    evidence_refs: testCase.evidence_refs,
    finished_at: new Date().toISOString(),
    fixture_ids: [],
    green_assertions: testCase.green_assertions,
    layer: testCase.layer,
    manifest,
    missing_evidence: missingEvidence,
    parent_family: testCase.parent_family,
    phase: "green",
    release_gate: ["work_package:WP-0", "release:P0-A"],
    requirements: testCase.requirements,
    run_id: runId,
    schema_version: "1.0",
    started_at: startedAt,
    status: passed ? "passed" : "failed",
    task_id: "TASK-WP0-02",
    test_id: testCase.test_id,
    work_package: "WP-0",
    worktree_under_test: worktreeDirty ? "uncommitted implementation" : "clean committed implementation",
  };
  writeFileSync(resolve(testCase.directory, "result.json"), `${JSON.stringify(result, null, 2)}\n`);
  return result;
});

const summary = {
  cases: results.map(({ missing_evidence, status, test_id }) => ({ missing_evidence, status, test_id })),
  run_id: runId,
  status: results.every((result) => result.status === "passed") ? "passed" : "failed",
};
writeFileSync(resolve(runDirectory, "evidence.json"), `${JSON.stringify(summary, null, 2)}\n`);
console.log(JSON.stringify(summary, null, 2));
if (summary.status !== "passed") process.exit(1);
