// Quality Intelligence evidence manifest shape (Issue #274, Epic #270, ADR-0023 D8).
//
// Versioned, persistable record the QI workflow writes through `keiko-evidence` after each run.
// Carries ONLY references and counts — never raw prompts, never raw source content, never
// credentials. The shape mirrors the Keiko `EvidenceManifest` evolution rule: a breaking change
// introduces a new `qiEvidenceSchemaVersion` literal member rather than mutating the existing one.
//
// Schema-version `1`: aligned with `QUALITY_INTELLIGENCE_AUDIT_MANIFEST_SCHEMA_VERSION` in
// `@oscharko-dev/keiko-contracts`. The audit summary is the cross-referenced run aggregate;
// this manifest is the persistable evidence wrapper around it.
export const QUALITY_INTELLIGENCE_EVIDENCE_SCHEMA_VERSION = 1;
// ─── Validation ────────────────────────────────────────────────────────────────────
// The set of allowed top-level keys. A persisted record carrying any extra key fails the
// strict-schema gate on read, matching the existing EvidenceManifest discipline. Update this set
// in lock-step with the interface above.
const ALLOWED_TOP_LEVEL_KEYS = new Set([
    "qiEvidenceSchemaVersion",
    "runId",
    "planAt",
    "completedAt",
    "status",
    "policyProfileIds",
    "retentionPolicyId",
    "modelGatewayCallCount",
    "totals",
    "findings",
    "exports",
    "evidenceRefs",
    "provenanceRefs",
    "redactionSummary",
    "integrityHashes",
    "coverageMatrix",
    "qualityScore",
    "sourceFingerprints",
    "atomFingerprints",
    "modelId",
    "modelParameters",
    "seedUsed",
]);
const ALLOWED_STATUSES = new Set([
    "running",
    "succeeded",
    "failed",
    "cancelled",
]);
// Strict-schema gate for a deserialised QI evidence record. Validates the schema-version literal,
// the closed set of top-level keys, and the status enum. Counts/integrity correctness is
// orthogonally enforced by the builder before persist.
export function validateQualityIntelligenceEvidenceManifest(value) {
    if (typeof value !== "object" || value === null) {
        return { ok: false, reason: "manifest is not an object" };
    }
    const record = value;
    if (record.qiEvidenceSchemaVersion !== QUALITY_INTELLIGENCE_EVIDENCE_SCHEMA_VERSION) {
        return {
            ok: false,
            reason: `unexpected qiEvidenceSchemaVersion (expected ${String(QUALITY_INTELLIGENCE_EVIDENCE_SCHEMA_VERSION)})`,
        };
    }
    for (const key of Object.keys(record)) {
        if (!ALLOWED_TOP_LEVEL_KEYS.has(key)) {
            return { ok: false, reason: `unknown manifest key: ${key}` };
        }
    }
    const status = record.status;
    if (typeof status !== "string" || !ALLOWED_STATUSES.has(status)) {
        return { ok: false, reason: "invalid status" };
    }
    return { ok: true, reason: undefined };
}
