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 ?? `wp2-02-${phase}-${new Date().toISOString().replace(/[^0-9]/g, "")}`;
const runDirectory = resolve("artifacts", "tdd", runId);
const casesRoot = resolve(runDirectory, "cases");
const playwrightDirectory = resolve(runDirectory, "playwright");
const caseIds = [
  "TDD-WP2-PROJ-002-cas-success",
  "TDD-WP2-PROJ-002-debounce",
  "TDD-WP2-PROJ-002-save-failure-recovery",
  "TDD-WP2-PROJ-004-conflict-export-once",
  "TDD-WP2-PROJ-004-stale-tab",
];
if (existsSync(runDirectory)) throw new Error(`Evidence run already exists: ${runId}`);
for (const caseId of caseIds) mkdirSync(resolve(casesRoot, caseId), { recursive: true });

const commandsToRun = phase === "red"
  ? [
      ["project-state-unit", ["exec", "vitest", "run", "tests/unit/wp2-02-autosave.test.ts"]],
      ["project-state-api", ["exec", "vitest", "run", "tests/api/wp2-02-project-state.test.ts"]],
      ["project-state-e2e", ["exec", "playwright", "test", "tests/e2e/project-autosave-conflict.spec.ts", "--config", "playwright.config.ts"]],
    ]
  : [
      ["unit", ["test:unit"]],
      ["api", ["test:api"]],
      ["e2e", ["test:e2e"]],
      ["tdd-trace", ["validate:tdd-trace"]],
    ];
const environment = {
  ...process.env,
  DADA_EVIDENCE_DIR_PROJECT_STATE: casesRoot,
  DADA_PLAYWRIGHT_OUTPUT_DIR: playwrightDirectory,
};
const commandResults = [];
for (const [name, args] of commandsToRun) {
  const command = `pnpm ${args.join(" ")}`;
  const started_at = new Date().toISOString();
  const execution = spawnSync(process.env.ComSpec ?? "cmd.exe", ["/d", "/s", "/c", command], { encoding: "utf8", env: environment });
  if (execution.stdout) process.stdout.write(execution.stdout);
  if (execution.stderr) process.stderr.write(execution.stderr);
  commandResults.push({ command, exit_code: execution.status ?? 1, finished_at: new Date().toISOString(), name, 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 traces = find(playwrightDirectory, "trace.zip");
  const recoveryTrace = traces.find((path) => path.includes("browser-draft-persistence"));
  const conflictTrace = traces.find((path) => path.includes("consumes-one-local-export"));
  if (recoveryTrace) {
    copyFileSync(recoveryTrace, resolve(casesRoot, caseIds[1], "trace.zip"));
    copyFileSync(recoveryTrace, resolve(casesRoot, caseIds[2], "trace.zip"));
  }
  if (conflictTrace) {
    copyFileSync(conflictTrace, resolve(casesRoot, caseIds[3], "trace.zip"));
    copyFileSync(conflictTrace, resolve(casesRoot, caseIds[4], "trace.zip"));
  }
}

for (const caseId of caseIds) {
  writeFileSync(resolve(casesRoot, caseId, "commands.json"), `${JSON.stringify({ commands: commandResults, phase, run_id: runId }, null, 2)}\n`);
}
const evidenceByCase = {
  "TDD-WP2-PROJ-002-cas-success": ["request.json", "response.json", "db-diff.json"],
  "TDD-WP2-PROJ-002-debounce": ["network-timeline.json", "response.json", "db-diff.json", "trace.zip"],
  "TDD-WP2-PROJ-002-save-failure-recovery": ["response.json", "db-diff.json", "cache-enumeration.json", "trace.zip", "screenshots/save-failed.png"],
  "TDD-WP2-PROJ-004-conflict-export-once": ["network-timeline.json", "db-diff.json", "trace.zip", "screenshots/conflict-export.png"],
  "TDD-WP2-PROJ-004-stale-tab": ["response.json", "db-diff.json", "trace.zip", "screenshots/conflicted.png"],
};
const commandState = phase === "red"
  ? commandResults.every((result) => result.exit_code !== 0)
  : commandResults.every((result) => result.exit_code === 0);
if (phase === "red") {
  for (const caseId of caseIds) {
    writeFileSync(resolve(casesRoot, caseId, "red-observation.json"), `${JSON.stringify({
      expected_failure: caseId.includes("PROJ-002")
        ? "Canvas schema, serial autosave, failure recovery and CAS are absent"
        : "stale-tab conflict lock and one-use local export are absent",
      status: commandState ? "red_confirmed" : "failed",
    }, 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 dirty = spawnSync("git", ["status", "--porcelain"], { encoding: "utf8" }).stdout.trim().length > 0;
const metadata = {
  "TDD-WP2-PROJ-002-cas-success": { acceptance_criteria: ["AC-20"], layer: ["API"], requirements: ["PROJECT-04"] },
  "TDD-WP2-PROJ-002-debounce": { acceptance_criteria: ["AC-20"], layer: ["UNIT", "E2E"], requirements: ["PROJECT-04", "PROJECT-06"] },
  "TDD-WP2-PROJ-002-save-failure-recovery": { acceptance_criteria: ["AC-20", "AC-35"], layer: ["UNIT", "E2E"], requirements: ["PROJECT-04", "PROJECT-05", "EXPORT-06"] },
  "TDD-WP2-PROJ-004-conflict-export-once": { acceptance_criteria: ["AC-36"], layer: ["E2E"], requirements: ["PROJECT-07", "EXPORT-05"] },
  "TDD-WP2-PROJ-004-stale-tab": { acceptance_criteria: ["AC-36"], layer: ["API", "E2E"], requirements: ["PROJECT-07"] },
};
const results = caseIds.map((testId) => {
  const directory = resolve(casesRoot, testId);
  const evidence_refs = phase === "red" ? ["red-observation.json"] : evidenceByCase[testId];
  const missing_evidence = evidence_refs.filter((file) => !existsSync(resolve(directory, file)));
  const status = commandState && missing_evidence.length === 0 ? (phase === "red" ? "red_confirmed" : "passed") : "failed";
  const result = {
    ...metadata[testId], automation: ["automated"], commit, evidence_refs, manifest, missing_evidence, phase,
    run_id: runId, status, task_id: "TASK-WP2-02", test_id: testId, work_package: "WP-2",
    worktree_under_test: dirty ? "uncommitted implementation" : "clean committed implementation",
  };
  writeFileSync(resolve(directory, "result.json"), `${JSON.stringify(result, null, 2)}\n`);
  return result;
});
const targetStatus = phase === "red" ? "red_confirmed" : "passed";
const passed = results.every((result) => result.status === targetStatus);
const summary = {
  cases: results.map(({ missing_evidence, status, test_id }) => ({ missing_evidence, status, test_id })),
  phase, run_id: runId, status: passed ? targetStatus : "failed",
};
writeFileSync(resolve(runDirectory, "evidence.json"), `${JSON.stringify(summary, null, 2)}\n`);
console.log(JSON.stringify(summary, null, 2));
if (!passed) process.exit(1);
