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

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 reviewed = [];
  const evidenceIds = new Set();
  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]);
    const complete = matrix.model_id === modelId && matrix.config_version === 1 && matrix.status === "passed"
      && matrix.pure_text?.status === "passed" && matrix.reference_image?.status === "passed"
      && matrix.ratios?.length === 4 && matrix.ratios.every((entry) => entry.status === "passed")
      && matrix.execution_modes?.length === 3 && matrix.execution_modes.every((entry) => ["passed", "covered_by_real_calls"].includes(entry.status))
      && matrix.error_scenarios?.length === 9 && matrix.error_scenarios.every((entry) => entry.status === "passed")
      && matrix.settlements?.length === 3 && matrix.contract_change?.full_matrix_reapplied === true
      && externalCalls.status === "passed" && externalCalls.real_calls === 5 && externalCalls.planned_real_calls === 5
      && externalCalls.calls?.length === 5 && new Set(externalCalls.calls.map((entry) => entry.scenario_id)).size === 5
      && externalCalls.calls.every((entry) => entry.status === "passed" && entry.source === "real_gateway")
      && externalCalls.approved_real_call_limit === 120
      && readiness.status === "passed" && redaction.status === "passed" && redaction.secret_scan === "passed";
    if (!complete || evidenceIds.has(readiness.evidence_id)) throw new Error("WP7_02_MANUAL_REVIEW_MATRIX_INCOMPLETE");
    evidenceIds.add(readiness.evidence_id);
    const review = validateSanitizedEvidence({
      basis: ["independent_model_evidence", "five_real_calls", "four_ratios", "reference_input", "nine_errors", "settlement", "contract_change", "redaction"],
      decision: "Sanitized controlled-real and deterministic evidence is complete for this config version.",
      model_id: modelId,
      reviewed_at: new Date().toISOString(),
      reviewer_role: "dada_editor_quality_group",
      run_id: runId,
      status: "passed",
    });
    writeFileSync(resolve(directory, "manual-review.json"), `${JSON.stringify(review, null, 2)}\n`);
    reviewed.push({ model_id: modelId, status: "passed" });
  }
  output({ reviewed, run_id: runId, status: "passed" });
} 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;
}
