{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:certarail:schema:event:certarail.eligibility.decision.v1",
  "title": "CertaRail eligibility decision event v1",
  "description": "Immutable event envelope written to the PostgreSQL outbox in the same transaction as the decision and audit receipt.",
  "type": "object",
  "additionalProperties": false,
  "required": [
    "event_id",
    "event_type",
    "occurred_at",
    "tenant_id",
    "result"
  ],
  "properties": {
    "event_id": {
      "type": "string",
      "pattern": "^evt_",
      "minLength": 5
    },
    "event_type": {
      "const": "certarail.eligibility.decision.v1"
    },
    "occurred_at": {
      "type": "string",
      "format": "date-time"
    },
    "tenant_id": {
      "type": "string",
      "minLength": 1
    },
    "result": {
      "$ref": "#/$defs/evaluationResult"
    }
  },
  "$defs": {
    "evaluationResult": {
      "type": "object",
      "additionalProperties": false,
      "required": ["decision", "audit"],
      "properties": {
        "decision": {
          "$ref": "#/$defs/decision"
        },
        "audit": {
          "$ref": "#/$defs/auditReceipt"
        }
      }
    },
    "decision": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "decision_id",
        "request_id",
        "tenant_id",
        "outcome",
        "enforceable",
        "evaluated_at",
        "operation",
        "asset_code",
        "input_digest",
        "checks"
      ],
      "properties": {
        "decision_id": {
          "type": "string",
          "minLength": 1
        },
        "request_id": {
          "type": "string",
          "minLength": 1
        },
        "tenant_id": {
          "type": "string",
          "minLength": 1
        },
        "outcome": {
          "enum": ["ALLOW", "DENY", "REVIEW"]
        },
        "enforceable": {
          "type": "boolean"
        },
        "evaluated_at": {
          "type": "string",
          "format": "date-time"
        },
        "evaluator_version": {
          "const": "certarail.eligibility.go.v1",
          "description": "Immutable rule-engine semantics identity. Optional in v1 only for pre-migration committed replays; every new decision includes it."
        },
        "operation": {
          "enum": ["BUY", "SELL", "DEPOSIT", "WITHDRAW", "EXCHANGE"]
        },
        "asset_code": {
          "type": "string",
          "minLength": 1
        },
        "policy_id": {
          "type": "string",
          "minLength": 1
        },
        "policy_version": {
          "type": "string",
          "minLength": 1
        },
        "policy_digest": {
          "$ref": "#/$defs/digest"
        },
        "policy_mode": {
          "enum": ["SANDBOX", "PRODUCTION"]
        },
        "input_digest": {
          "$ref": "#/$defs/digest"
        },
        "checks": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/check"
          }
        },
        "obligations": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "check": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "status", "summary"],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1
        },
        "status": {
          "enum": ["PASS", "FAIL", "REVIEW"]
        },
        "summary": {
          "type": "string"
        },
        "rule_id": {
          "type": "string",
          "minLength": 1
        },
        "source_refs": {
          "type": "array",
          "items": {
            "type": "string"
          }
        }
      }
    },
    "auditReceipt": {
      "type": "object",
      "additionalProperties": false,
      "required": ["lane", "sequence", "recorded_at", "event_hash"],
      "properties": {
        "lane": {
          "type": "integer",
          "minimum": 0,
          "maximum": 31
        },
        "sequence": {
          "type": "integer",
          "minimum": 1
        },
        "recorded_at": {
          "type": "string",
          "format": "date-time"
        },
        "evaluator_version": {
          "const": "certarail.eligibility.go.v1",
          "description": "Evaluator identity bound into the audit event hash. Optional in v1 only for pre-migration committed replays; every new audit event includes it."
        },
        "previous_hash": {
          "$ref": "#/$defs/digest"
        },
        "event_hash": {
          "$ref": "#/$defs/digest"
        }
      }
    },
    "digest": {
      "type": "string",
      "pattern": "^[0-9a-f]{64}$"
    }
  }
}
