{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://finance.entelekron.org/schemas/reconciliation-journal-entry.schema.json",
  "title": "Future append-only reconciliation journal entry",
  "description": "Schema cannot calculate debit-credit equality; validators must enforce the declared balance invariant.",
  "type": "object",
  "additionalProperties": false,
  "x-tvkusd-invariants": ["sum(debit lines) equals sum(credit lines)", "all lines share currency and scale", "corrections link reversing and replacement entries"],
  "required": ["schemaVersion", "entryId", "sequence", "journalId", "environment", "effectiveAt", "recordedAt", "orderReference", "policyBundleDigest", "evidenceDigest", "sourceFinality", "lines", "previousEntryDigest", "entryDigest", "approvals"],
  "properties": {
    "schemaVersion": {"type": "string", "pattern": "^[0-9]+\\.[0-9]+\\.[0-9]+$"},
    "entryId": {"type": "string", "minLength": 16, "maxLength": 128},
    "sequence": {"type": "integer", "minimum": 1},
    "journalId": {"type": "string", "minLength": 1},
    "environment": {"enum": ["sandbox", "production"]},
    "effectiveAt": {"type": "string", "format": "date-time"},
    "recordedAt": {"type": "string", "format": "date-time"},
    "orderReference": {"type": "string", "minLength": 1},
    "policyBundleDigest": {"type": "string", "pattern": "^[a-f0-9]{64}$"},
    "evidenceDigest": {"type": "string", "pattern": "^[a-f0-9]{64}$"},
    "sourceFinality": {
      "type": "object", "additionalProperties": false,
      "required": ["bank", "chain", "provider"],
      "properties": {
        "bank": {"type": ["string", "null"], "maxLength": 256},
        "chain": {"type": ["string", "null"], "maxLength": 256},
        "provider": {"type": ["string", "null"], "maxLength": 256}
      }
    },
    "lines": {
      "type": "array", "minItems": 2, "maxItems": 100,
      "items": {
        "type": "object", "additionalProperties": false,
        "required": ["lineId", "bookId", "accountReference", "side", "amount", "currency", "scale", "legalOwnerReference", "purpose"],
        "properties": {
          "lineId": {"type": "string", "minLength": 1}, "bookId": {"type": "string", "minLength": 1}, "accountReference": {"type": "string", "minLength": 1},
          "side": {"enum": ["debit", "credit"]}, "amount": {"type": "string", "pattern": "^(0|[1-9][0-9]*)(\\.[0-9]+)?$"}, "currency": {"type": "string", "pattern": "^[A-Z0-9]{3,12}$"}, "scale": {"type": "integer", "minimum": 0, "maximum": 18},
          "legalOwnerReference": {"type": "string", "minLength": 1}, "purpose": {"type": "string", "minLength": 1}
        }
      }
    },
    "previousEntryDigest": {"type": ["string", "null"], "pattern": "^[a-f0-9]{64}$"},
    "entryDigest": {"type": "string", "pattern": "^[a-f0-9]{64}$"},
    "approvals": {
      "type": "array", "minItems": 2, "uniqueItems": true,
      "items": {"type": "object", "additionalProperties": false, "required": ["approvalId", "domain", "approvedAt", "intentDigest"], "properties": {"approvalId": {"type": "string", "minLength": 1}, "domain": {"type": "string", "minLength": 1}, "approvedAt": {"type": "string", "format": "date-time"}, "intentDigest": {"type": "string", "pattern": "^[a-f0-9]{64}$"}}}
    }
  }
}
