import { createHash } from "node:crypto";
import { spawnSync } from "node:child_process";
import { copyFileSync, existsSync, mkdirSync, readdirSync, readFileSync, 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 ?? `wp4-03-${phase}-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const casesDirectory = resolve(runDirectory, "cases");
if (existsSync(runDirectory)) throw new Error(`Evidence run already exists: ${runId}`);
mkdirSync(casesDirectory, { recursive: true });

const cases = [
  { acceptance_criteria: ["AC-09", "AC-10"], evidence: ["canvas-state.json", "db-diff.json", "pixel-diff.json", "trace.zip"], id: "TDD-WP4-TXT-001-multiline-template-switch", requirements: ["TEXT-01", "TEXT-02", "TEXT-03", "TEXT-04", "TEXT-05", "TEXT-06", "PROJECT-04"] },
  { acceptance_criteria: ["AC-11", "AC-12"], evidence: ["font-load.json", "canvas-state.json", "pixel-diff.json", "trace.zip"], id: "TDD-WP4-TXT-002-font-metrics-ranges", requirements: ["TEXT-07", "TEXT-08", "TEXT-09", "TEXT-10", "TEXT-11", "TEXT-12", "TEXT-13", "TEXT-14", "NFR-02"] },
  { acceptance_criteria: ["AC-09", "AC-32"], evidence: ["catalog.json", "response.json", "db-diff.json", "trace.zip"], id: "TDD-WP4-TXT-003-catalog-search-recent", requirements: ["TEXT-01", "TEXT-02"] },
];
for (const item of cases) mkdirSync(resolve(casesDirectory, item.id), { recursive: true });

const outputDirectory = resolve(runDirectory, "playwright-output");
const archiveEvidence = resolve(runDirectory, "font-source.json");
const environment = {
  ...process.env,
  DADA_EVIDENCE_DIR_TEXT_EDITOR: casesDirectory,
  DADA_FONT_SOURCE_EVIDENCE: archiveEvidence,
  DADA_PLAYWRIGHT_OUTPUT_DIR: outputDirectory,
};
const commands = phase === "red" ? [] : [
  ["font-source", "pnpm exec node scripts/verify-wp4-03-font-source.mjs"],
  ["unit", "pnpm test:unit"],
  ["integration", "pnpm test:integration"],
  ["api", "pnpm test:api"],
  ["e2e", "pnpm test:e2e"],
  ["visual", "pnpm test:visual"],
  ["performance", "pnpm test:performance"],
  ["tdd-trace", "pnpm validate:tdd-trace"],
];
const commandResults = [];
for (const [name, command] of commands) {
  const started_at = new Date().toISOString();
  const result = spawnSync(process.env.ComSpec ?? "cmd.exe", ["/d", "/s", "/c", command], { encoding: "utf8", env: environment });
  if (result.stdout) process.stdout.write(result.stdout);
  if (result.stderr) process.stderr.write(result.stderr);
  commandResults.push({ command, exit_code: result.status ?? 1, finished_at: new Date().toISOString(), name, started_at });
}

function findTraces(directory) {
  const traces = [];
  if (!existsSync(directory)) return traces;
  for (const entry of readdirSync(directory, { withFileTypes: true })) {
    const path = resolve(directory, entry.name);
    if (entry.isDirectory()) traces.push(...findTraces(path));
    else if (entry.name === "trace.zip") traces.push(path);
  }
  return traces;
}
if (phase === "green") {
  const traces = findTraces(outputDirectory);
  const mappings = ["template-switch", "style-ranges-once", "account-recent-use"];
  mappings.forEach((needle, index) => {
    const trace = traces.find((path) => path.includes(needle));
    if (trace) copyFileSync(trace, resolve(casesDirectory, cases[index].id, "trace.zip"));
  });
  const fontEvidencePath = resolve(casesDirectory, cases[1].id, "font-load.json");
  if (existsSync(fontEvidencePath) && existsSync(archiveEvidence)) {
    const browser = JSON.parse(readFileSync(fontEvidencePath, "utf8"));
    const archive = JSON.parse(readFileSync(archiveEvidence, "utf8"));
    writeFileSync(fontEvidencePath, `${JSON.stringify({ ...browser, archive_verification: archive }, null, 2)}\n`);
  }
}

const commandState = phase === "red" ? true : commandResults.every((result) => result.exit_code === 0);
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 dirty = spawnSync("git", ["status", "--porcelain"], { encoding: "utf8" }).stdout.trim().length > 0;
if (phase === "red") {
  const observation = {
    expected_failure: "Text template domain, font gate, recent asset service and enabled editor entry were absent before TASK-WP4-03",
    observed_commands: [
      "pnpm vitest run tests/unit/wp4-03-text-editor.test.ts tests/integration/wp4-03-recent-assets.test.ts",
      "pnpm vitest run tests/unit/wp4-03-font-loader.test.ts",
      "pnpm vitest run tests/api/wp4-03-recent-assets.test.ts",
      "pnpm playwright test tests/e2e/wp4-03-text-editor.spec.ts --grep frozen catalog",
    ],
    observed_errors: ["Cannot find module text-assets.js", "Cannot find module recent-assets.js", "Cannot find module text-font-loader.js", "recent assets endpoint returned 404", "文字模板 button was disabled"],
    status: "red_confirmed",
  };
  for (const item of cases) writeFileSync(resolve(casesDirectory, item.id, "red-observation.json"), `${JSON.stringify(observation, null, 2)}\n`);
}

const summaries = [];
for (const item of cases) {
  const directory = resolve(casesDirectory, item.id);
  const evidenceRefs = phase === "red" ? ["red-observation.json"] : item.evidence;
  const missingEvidence = evidenceRefs.filter((file) => !existsSync(resolve(directory, file)));
  const status = commandState && missingEvidence.length === 0 ? phase === "red" ? "red_confirmed" : "passed" : "failed";
  writeFileSync(resolve(directory, "commands.json"), `${JSON.stringify({ commands: commandResults, phase, run_id: runId }, null, 2)}\n`);
  writeFileSync(resolve(directory, "result.json"), `${JSON.stringify({
    acceptance_criteria: item.acceptance_criteria, automation: ["automated"], commit, evidence_refs: evidenceRefs,
    layer: ["UNIT", "E2E", "VIS-PERF"], layer_notes: { PERFORMANCE: "current root runner reports not_applicable for TASK-WP0-01", VISUAL: "current root runner reports not_applicable for TASK-WP0-01; task screenshots and pixel evidence are generated by the focused E2E" },
    manifest, missing_evidence: missingEvidence, phase, requirements: item.requirements, run_id: runId, status,
    task_id: "TASK-WP4-03", test_id: item.id, work_package: "WP-4", worktree_under_test: dirty ? "uncommitted implementation" : "clean committed implementation",
  }, null, 2)}\n`);
  summaries.push({ missing_evidence: missingEvidence, status, test_id: item.id });
}
const targetStatus = phase === "red" ? "red_confirmed" : "passed";
const status = summaries.every((item) => item.status === targetStatus) ? targetStatus : "failed";
writeFileSync(resolve(runDirectory, "commands.json"), `${JSON.stringify({ commands: commandResults, phase, run_id: runId }, null, 2)}\n`);
writeFileSync(resolve(runDirectory, "evidence.json"), `${JSON.stringify({ cases: summaries, phase, run_id: runId, status }, null, 2)}\n`);
console.log(JSON.stringify({ cases: summaries, phase, run_id: runId, status }, null, 2));
if (status !== targetStatus) process.exit(1);
