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-03-green-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const boundaryCaseId = "TDD-WP0-BND-001-loopback-origin";
const supportedCaseId = "TDD-WP0-BRW-001-real-supported";
const blockedCaseId = "TDD-WP0-BRW-002-hard-block";
const boundaryDirectory = resolve(runDirectory, "cases", boundaryCaseId);
const supportedDirectory = resolve(runDirectory, "cases", supportedCaseId);
const blockedDirectory = resolve(runDirectory, "cases", blockedCaseId);
const playwrightDirectory = resolve(runDirectory, "playwright");

if (existsSync(runDirectory)) throw new Error(`Evidence run already exists: ${runId}`);
for (const directory of [boundaryDirectory, supportedDirectory, blockedDirectory]) {
  mkdirSync(directory, { recursive: true });
}

const commandDefinitions = [
  { command: "pnpm test:api", args: ["test:api"] },
  { command: "pnpm test:security", args: ["test:security"] },
  { command: "pnpm test:package", args: ["test:package"] },
  { 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_BND: boundaryDirectory,
      DADA_EVIDENCE_DIR_BRW_BLOCKED: blockedDirectory,
      DADA_EVIDENCE_DIR_BRW_SUPPORTED: supportedDirectory,
      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 findFiles(root, target) {
  if (!existsSync(root)) return [];
  const files = [];
  for (const name of readdirSync(root)) {
    const child = resolve(root, name);
    if (statSync(child).isDirectory()) files.push(...findFiles(child, target));
    else if (name === target) files.push(child);
  }
  return files;
}

for (const trace of findFiles(playwrightDirectory, "trace.zip")) {
  const normalized = trace.replaceAll("\\", "/");
  if (normalized.includes("support-gate-a-real-Edge")) {
    const target = resolve(supportedDirectory, "edge", "trace.zip");
    mkdirSync(resolve(supportedDirectory, "edge"), { recursive: true });
    copyFileSync(trace, target);
  }
  if (normalized.includes("support-gate-the-hard-bloc")) {
    copyFileSync(trace, resolve(blockedDirectory, "trace.zip"));
  }
}

const chromeDirectory = resolve(supportedDirectory, "chrome");
mkdirSync(resolve(chromeDirectory, "screenshots"), { recursive: true });
const chromeCandidates = [
  "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
  "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
];
const chromeInstalled = chromeCandidates.some((path) => existsSync(path));
writeFileSync(
  resolve(chromeDirectory, "environment.json"),
  `${JSON.stringify({ browser: "Google Chrome", final_release: false, installed: chromeInstalled, status: "pending_manual" }, null, 2)}\n`,
);
writeFileSync(
  resolve(chromeDirectory, "response.json"),
  `${JSON.stringify({ reason: "Final RELEASE.json and real Chrome validation belong to WP-7.", status: "not_run" }, null, 2)}\n`,
);

const commandEvidence = { commands, phase: "green", run_id: runId, schema_version: "1.0" };
for (const directory of [boundaryDirectory, supportedDirectory, blockedDirectory]) {
  writeFileSync(resolve(directory, "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 commandsPassed = commands.every(({ exit_code }) => exit_code === 0);

function writeResult(directory, result) {
  writeFileSync(resolve(directory, "result.json"), `${JSON.stringify(result, null, 2)}\n`);
  return result;
}

const boundaryEvidence = ["socket-listeners.json", "response.json", "firewall-diff.json"];
const boundaryMissing = boundaryEvidence.filter((file) => !existsSync(resolve(boundaryDirectory, file)));
const boundaryResult = writeResult(boundaryDirectory, {
  acceptance_criteria: ["AC-24"],
  automation: ["automated"],
  commit,
  environment,
  evidence_refs: boundaryEvidence,
  finished_at: new Date().toISOString(),
  layer: ["API", "PKG-SEC"],
  manifest,
  missing_evidence: boundaryMissing,
  parent_family: "TDD-WP0-BND-001",
  phase: "green",
  release_gate: ["work_package:WP-0", "release:P0-A"],
  requirements: ["NFR-09"],
  run_id: runId,
  schema_version: "1.0",
  started_at: startedAt,
  status: commandsPassed && boundaryMissing.length === 0 ? "passed" : "failed",
  task_id: "TASK-WP0-03",
  test_id: boundaryCaseId,
  work_package: "WP-0",
  worktree_under_test: worktreeDirty ? "uncommitted implementation" : "clean committed implementation",
});

const blockedEvidence = [
  "response.json",
  "db-access.json",
  "external-calls.json",
  "trace.zip",
  "screenshots/blocked.png",
];
const blockedMissing = blockedEvidence.filter((file) => !existsSync(resolve(blockedDirectory, file)));
const blockedResult = writeResult(blockedDirectory, {
  acceptance_criteria: ["AC-24"],
  automation: ["automated"],
  commit,
  environment,
  evidence_refs: blockedEvidence,
  finished_at: new Date().toISOString(),
  layer: ["API", "E2E"],
  manifest,
  missing_evidence: blockedMissing,
  parent_family: "TDD-WP0-BRW-002",
  phase: "green",
  release_gate: ["work_package:WP-0", "release:P0-A"],
  requirements: ["NFR-01"],
  run_id: runId,
  schema_version: "1.0",
  started_at: startedAt,
  status: commandsPassed && blockedMissing.length === 0 ? "passed" : "failed",
  task_id: "TASK-WP0-03",
  test_id: blockedCaseId,
  work_package: "WP-0",
  worktree_under_test: worktreeDirty ? "uncommitted implementation" : "clean committed implementation",
});

const supportedAutomatedEvidence = [
  "edge/environment.json",
  "edge/response.json",
  "edge/trace.zip",
  "edge/screenshots/supported.png",
];
const supportedAutomatedMissing = supportedAutomatedEvidence.filter(
  (file) => !existsSync(resolve(supportedDirectory, file)),
);
const supportedExternalMissing = [
  "final/RELEASE.json",
  "chrome/trace.zip",
  "chrome/screenshots/supported.png",
  "final Chrome/Edge AC-24 evidence",
];
const supportedResult = writeResult(supportedDirectory, {
  acceptance_criteria: ["AC-24", "AC-41"],
  automation: ["automated", "manual_review"],
  automation_status: commandsPassed && supportedAutomatedMissing.length === 0 ? "passed" : "failed",
  commit,
  environment,
  evidence_refs: [
    ...supportedAutomatedEvidence,
    "chrome/environment.json",
    "chrome/response.json",
  ],
  external_blockers: [
    "Final RELEASE.json is created only after WP-7 candidate and AC validation.",
    "A real installed Chrome full-version run is not available in this workspace.",
    "The Edge run uses a test candidate release and cannot replace final release evidence.",
  ],
  finished_at: new Date().toISOString(),
  layer: ["E2E", "MANUAL"],
  manifest,
  missing_evidence: supportedExternalMissing,
  missing_automated_evidence: supportedAutomatedMissing,
  parent_family: "TDD-WP0-BRW-001",
  phase: "green",
  release_gate: ["work_package:WP-0", "release:P0-A"],
  requirements: ["NFR-01"],
  run_id: runId,
  schema_version: "1.0",
  started_at: startedAt,
  status: commandsPassed && supportedAutomatedMissing.length === 0 ? "externally_blocked" : "failed",
  task_id: "TASK-WP0-03",
  test_id: supportedCaseId,
  work_package: "WP-0",
  worktree_under_test: worktreeDirty ? "uncommitted implementation" : "clean committed implementation",
});

const automatedPassed = boundaryResult.status === "passed" && blockedResult.status === "passed" && supportedResult.automation_status === "passed";
const summary = {
  cases: [boundaryResult, supportedResult, blockedResult].map(({ missing_evidence, status, test_id }) => ({
    missing_evidence,
    status,
    test_id,
  })),
  run_id: runId,
  status: automatedPassed ? "green_with_external_block" : "failed",
};
writeFileSync(resolve(runDirectory, "evidence.json"), `${JSON.stringify(summary, null, 2)}\n`);
console.log(JSON.stringify(summary, null, 2));
if (!automatedPassed) process.exit(1);
