import { WP7_02_MODEL_IDS } from "./wp7-02-external-contract.mjs";

const productDimensions = Object.freeze({
  "3:4": [1080, 1440],
  "1:1": [1080, 1080],
  "4:3": [1440, 1080],
  "9:16": [1080, 1920],
});

function modelEvidenceComplete(entry) {
  const { externalCalls, matrix, modelId, readiness, redaction } = entry;
  return matrix.model_id === modelId && Number.isSafeInteger(matrix.config_version) && matrix.config_version > 0 && matrix.status === "passed"
    && matrix.pure_text?.status === "passed" && matrix.reference_image?.status === "passed"
    && matrix.ratios?.length === 4 && matrix.ratios.every((row) => row.status === "passed")
    && matrix.execution_modes?.length === 3 && matrix.execution_modes.every((row) => ["passed", "covered_by_real_calls"].includes(row.status))
    && matrix.error_scenarios?.length === 9 && matrix.error_scenarios.every((row) => row.status === "passed")
    && matrix.settlements?.length === 3 && matrix.contract_change?.full_matrix_reapplied === true
    && externalCalls.status === "passed" && externalCalls.real_calls >= 5 && externalCalls.real_calls <= 6
    && externalCalls.planned_real_calls === 5 && externalCalls.maximum_real_calls === 6
    && externalCalls.attempts?.length === externalCalls.real_calls
    && externalCalls.calls?.length === 5 && new Set(externalCalls.calls.map((row) => row.scenario_id)).size === 5
    && externalCalls.calls.every((row) => row.status === "passed" && row.source === "real_gateway")
    && externalCalls.calls.every((row) => {
      const [width, height] = productDimensions[row.requested_ratio] ?? [];
      return row.response?.dimensions?.width === width && row.response?.dimensions?.height === height;
    })
    && externalCalls.approved_real_call_limit === 120
    && readiness.status === "passed" && redaction.status === "passed" && redaction.secret_scan === "passed";
}

export function reviewIndependentModelEvidence(entries, { reviewedAt, runId }) {
  const entryIds = Array.isArray(entries) ? entries.map((entry) => entry?.modelId).toSorted() : [];
  if (!Array.isArray(entries) || entries.length !== WP7_02_MODEL_IDS.length
    || JSON.stringify(entryIds) !== JSON.stringify([...WP7_02_MODEL_IDS].toSorted())
    || !runId || !reviewedAt) {
    throw new Error("WP7_02_MANUAL_REVIEW_EVIDENCE_INVALID");
  }
  const evidenceIds = entries.map((entry) => entry.readiness?.evidence_id);
  if (evidenceIds.some((id) => typeof id !== "string") || new Set(evidenceIds).size !== entries.length) {
    throw new Error("WP7_02_MANUAL_REVIEW_EVIDENCE_NOT_INDEPENDENT");
  }
  const reviews = entries.map((entry) => modelEvidenceComplete(entry) ? {
    basis: ["independent_model_evidence", "five_scenarios_bounded_attempts", "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: entry.modelId,
    reviewed_at: reviewedAt,
    reviewer_role: "dada_editor_quality_group",
    run_id: runId,
    status: "passed",
  } : {
    decision: "Independent model evidence remains incomplete or externally blocked.",
    model_id: entry.modelId,
    reviewed_at: reviewedAt,
    reviewer_role: "dada_editor_quality_group",
    run_id: runId,
    status: "blocked",
  });
  return { reviews, status: reviews.every((review) => review.status === "passed") ? "passed" : "externally_blocked" };
}
