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 phaseArgument = process.argv.indexOf("--phase");
const phase = phaseArgument >= 0 ? process.argv[phaseArgument + 1] : "green";
if (phase !== "red" && phase !== "green") throw new Error(`Unsupported phase: ${phase}`);

const runId = process.env.DADA_TDD_RUN_ID ?? `wp0-04-${phase}-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const rootCaseId = "TDD-WP0-DATA-001-root-validation";
const transferCaseId = "TDD-WP0-DATA-002-no-backup-migration";
const rootDirectory = resolve(runDirectory, "cases", rootCaseId);
const transferDirectory = resolve(runDirectory, "cases", transferCaseId);
const playwrightDirectory = resolve(runDirectory, "playwright");

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

const redCommands = [
  { command: "pnpm exec vitest run tests/integration/wp0-04-local-data-root.test.ts", args: ["exec", "vitest", "run", "tests/integration/wp0-04-local-data-root.test.ts"] },
  { command: "pnpm exec vitest run tests/api/wp0-04-resource-boundary.test.ts", args: ["exec", "vitest", "run", "tests/api/wp0-04-resource-boundary.test.ts"] },
  { command: "pnpm exec playwright test tests/e2e/local-data-boundary.spec.ts --config playwright.config.ts", args: ["exec", "playwright", "test", "tests/e2e/local-data-boundary.spec.ts", "--config", "playwright.config.ts"] },
];
const greenCommands = [
  { command: "pnpm test:integration", args: ["test:integration"] },
  { 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 phase === "red" ? redCommands : greenCommands) {
  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, {
    encoding: "utf8",
    env: {
      ...process.env,
      DADA_EVIDENCE_DIR_DATA_ROOT: rootDirectory,
      DADA_EVIDENCE_DIR_NO_TRANSFER: transferDirectory,
      DADA_PLAYWRIGHT_OUTPUT_DIR: playwrightDirectory,
    },
  });
  if (execution.stdout) process.stdout.write(execution.stdout);
  if (execution.stderr) process.stderr.write(execution.stderr);
  commands.push({
    command: definition.command,
    exit_code: execution.status ?? 1,
    finished_at: new Date().toISOString(),
    started_at: commandStartedAt,
  });
}

const commandEvidence = { commands, phase, run_id: runId, schema_version: "1.0" };
for (const directory of [rootDirectory, transferDirectory]) {
  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 environment = { arch: process.arch, node: process.version.slice(1), os: process.platform };

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;
}

if (phase === "green") {
  for (const trace of findFiles(playwrightDirectory, "trace.zip")) {
    if (trace.replaceAll("\\", "/").includes("local-data-boundary")) {
      copyFileSync(trace, resolve(transferDirectory, "trace.zip"));
    }
  }
}

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

const common = {
  commit,
  environment,
  finished_at: new Date().toISOString(),
  manifest,
  phase,
  release_gate: ["work_package:WP-0", "release:P0-A"],
  run_id: runId,
  schema_version: "1.0",
  started_at: startedAt,
  task_id: "TASK-WP0-04",
  work_package: "WP-0",
  worktree_under_test: worktreeDirty ? "uncommitted implementation" : "clean committed implementation",
};

let rootResult;
let transferResult;
if (phase === "red") {
  const redConfirmed = commands.every(({ exit_code }) => exit_code !== 0);
  rootResult = writeResult(rootDirectory, {
    ...common,
    acceptance_criteria: ["AC-41", "AC-55", "AC-56"],
    expected_failure: "LocalDataRoot validation, data_missing and stable resource isolation are not implemented.",
    layer: ["DB", "API", "PKG-SEC"],
    parent_family: "TDD-WP0-DATA-001",
    requirements: ["NFR-09", "17.19"],
    status: redConfirmed ? "red_confirmed" : "failed",
    test_id: rootCaseId,
  });
  transferResult = writeResult(transferDirectory, {
    ...common,
    acceptance_criteria: ["AC-24", "AC-56"],
    expected_failure: "No-backup/no-migration policy and fixed settings copy are not implemented.",
    layer: ["API", "E2E", "MANUAL"],
    parent_family: "TDD-WP0-DATA-002",
    requirements: ["AUTH-06", "NFR-09"],
    status: redConfirmed ? "red_confirmed" : "failed",
    test_id: transferCaseId,
  });
} else {
  const commandsPassed = commands.every(({ exit_code }) => exit_code === 0);
  const rootEvidence = ["config-result.json", "fs-before.json", "fs-after.json", "response.json"];
  const transferEvidence = ["route-inventory.json", "package-scan.json", "trace.zip", "screenshots/fixed-copy.png"];
  const rootMissing = rootEvidence.filter((file) => !existsSync(resolve(rootDirectory, file)));
  const transferMissing = transferEvidence.filter((file) => !existsSync(resolve(transferDirectory, file)));
  rootResult = writeResult(rootDirectory, {
    ...common,
    acceptance_criteria: ["AC-41", "AC-55", "AC-56"],
    automation: ["automated"],
    evidence_refs: rootEvidence,
    layer: ["DB", "API", "PKG-SEC"],
    missing_evidence: rootMissing,
    parent_family: "TDD-WP0-DATA-001",
    requirements: ["NFR-09", "17.19"],
    status: commandsPassed && rootMissing.length === 0 ? "passed" : "failed",
    test_id: rootCaseId,
  });
  transferResult = writeResult(transferDirectory, {
    ...common,
    acceptance_criteria: ["AC-24", "AC-56"],
    automation: ["automated", "manual_review"],
    evidence_refs: transferEvidence,
    layer: ["API", "E2E", "MANUAL"],
    missing_evidence: transferMissing,
    parent_family: "TDD-WP0-DATA-002",
    requirements: ["AUTH-06", "NFR-09"],
    status: commandsPassed && transferMissing.length === 0 ? "passed" : "failed",
    test_id: transferCaseId,
  });
}

const passed = phase === "red"
  ? rootResult.status === "red_confirmed" && transferResult.status === "red_confirmed"
  : rootResult.status === "passed" && transferResult.status === "passed";
const summary = {
  cases: [rootResult, transferResult].map(({ status, test_id }) => ({ 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);
