{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "urn:verahelm:decision-envelope:1.0",
  "title": "Verahelm Decision Envelope",
  "type": "object",
  "additionalProperties": false,
  "required": ["payload", "signature"],
  "properties": {
    "payload": {
      "type": "object",
      "additionalProperties": false,
      "required": ["schema_version", "envelope_id", "subject", "authority", "scope", "evidence_refs", "decision", "lifecycle", "issuer"],
      "properties": {
        "schema_version": { "const": "1.0.0" },
        "envelope_id": { "type": "string", "pattern": "^de_[a-z0-9_]{8,80}$" },
        "subject": {
          "type": "object",
          "additionalProperties": false,
          "required": ["kind", "id", "version"],
          "properties": {
            "kind": { "type": "string", "minLength": 1, "maxLength": 64 },
            "id": { "type": "string", "minLength": 1, "maxLength": 160 },
            "version": { "$ref": "#/$defs/digest" }
          }
        },
        "authority": {
          "type": "object",
          "additionalProperties": false,
          "required": ["type", "id"],
          "properties": {
            "type": { "const": "customer" },
            "id": { "type": "string", "minLength": 1, "maxLength": 160 }
          }
        },
        "scope": {
          "type": "object",
          "additionalProperties": false,
          "required": ["environment", "change"],
          "properties": {
            "environment": { "type": "string", "minLength": 1, "maxLength": 80 },
            "change": { "type": "string", "minLength": 1, "maxLength": 240 }
          }
        },
        "evidence_refs": {
          "type": "array",
          "minItems": 1,
          "maxItems": 64,
          "uniqueItems": true,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["kind", "digest"],
            "properties": {
              "kind": { "type": "string", "minLength": 1, "maxLength": 64 },
              "digest": { "$ref": "#/$defs/digest" }
            }
          }
        },
        "decision": {
          "type": "object",
          "additionalProperties": false,
          "required": ["status", "conditions"],
          "properties": {
            "status": { "enum": ["pass", "blocked"] },
            "conditions": {
              "type": "array",
              "maxItems": 32,
              "uniqueItems": true,
              "items": { "type": "string", "minLength": 1, "maxLength": 320 }
            }
          }
        },
        "lifecycle": {
          "type": "object",
          "additionalProperties": false,
          "required": ["issued_at", "expires_at"],
          "properties": {
            "issued_at": { "$ref": "#/$defs/timestamp" },
            "expires_at": { "$ref": "#/$defs/timestamp" },
            "revoked_at": { "$ref": "#/$defs/timestamp" },
            "supersedes": { "type": "string", "pattern": "^de_[a-z0-9_]{8,80}$" },
            "superseded_by": { "type": "string", "pattern": "^de_[a-z0-9_]{8,80}$" }
          }
        },
        "issuer": {
          "type": "object",
          "additionalProperties": false,
          "required": ["id", "key_id"],
          "properties": {
            "id": { "type": "string", "minLength": 1, "maxLength": 160 },
            "key_id": { "type": "string", "minLength": 1, "maxLength": 80 }
          }
        }
      }
    },
    "signature": {
      "type": "object",
      "additionalProperties": false,
      "required": ["algorithm", "key_id", "value"],
      "properties": {
        "algorithm": { "const": "Ed25519" },
        "key_id": { "type": "string", "minLength": 1, "maxLength": 80 },
        "value": { "type": "string", "pattern": "^[A-Za-z0-9+/]{86}==$" }
      }
    },
    "status_url": {
      "type": "string",
      "pattern": "^/v1/decision-envelopes/de_[a-z0-9_]{8,80}/status$"
    }
  },
  "$defs": {
    "digest": {
      "type": "string",
      "pattern": "^sha256:[a-f0-9]{64}$"
    },
    "timestamp": {
      "type": "string",
      "format": "date-time",
      "pattern": "^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(?:\\.\\d{1,3})?Z$"
    }
  }
}
