import { existsSync, readFileSync, writeFileSync } from "node:fs";
import { resolve } from "node:path";

import { WP7_02_MODEL_IDS } from "./lib/wp7-02-external-contract.mjs";
import { validateSanitizedEvidence } from "./lib/wp7-02-controlled-executor.mjs";
import { reviewIndependentModelEvidence } from "./lib/wp7-02-manual-review.mjs";

const caseDirectory = process.env.DADA_WP7_02_CASE_DIR;
const runId = process.env.DADA_TDD_RUN_ID;
const confirmed = process.argv.includes("--confirm-manual-review");

function readJson(path) {
  return validateSanitizedEvidence(JSON.parse(readFileSync(path, "utf8")));
}

function output(value, error = false) {
  const serialized = JSON.stringify(validateSanitizedEvidence(value));
  if (error) console.error(serialized); else console.log(serialized);
}

try {
  if (!confirmed || !caseDirectory || !runId) throw new Error("WP7_02_MANUAL_REVIEW_CONFIRMATION_REQUIRED");
  const entries = [];
  for (const modelId of WP7_02_MODEL_IDS) {
    const directory = resolve(caseDirectory, modelId.replaceAll(".", "_"));
    const paths = ["contract-matrix.json", "external-calls.json", "readiness.json", "redaction.json"]
      .map((name) => resolve(directory, name));
    if (paths.some((path) => !existsSync(path))) throw new Error("WP7_02_MANUAL_REVIEW_EVIDENCE_MISSING");
    const matrix = readJson(paths[0]);
    const externalCalls = readJson(paths[1]);
    const readiness = readJson(paths[2]);
    const redaction = readJson(paths[3]);
    entries.push({ directory, externalCalls, matrix, modelId, readiness, redaction });
  }
  const result = reviewIndependentModelEvidence(entries, { reviewedAt: new Date().toISOString(), runId });
  for (let index = 0; index < entries.length; index += 1) {
    const review = validateSanitizedEvidence(result.reviews[index]);
    writeFileSync(resolve(entries[index].directory, "manual-review.json"), `${JSON.stringify(review, null, 2)}\n`);
  }
  output({ reviewed: result.reviews.map(({ model_id, status }) => ({ model_id, status })), run_id: runId, status: result.status }, result.status !== "passed");
  if (result.status !== "passed") process.exitCode = 3;
} catch (error) {
  const message = error instanceof Error ? error.message : String(error);
  const code = /^WP7_02_[A-Z0-9_:]+$/.test(message) ? message : "WP7_02_MANUAL_REVIEW_FAILED";
  output({ code, run_id: runId, status: "externally_blocked" }, true);
  process.exitCode = 3;
}
