{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://uwmd.org/schemas/uw-document-envelope.schema.json",
  "title": "UW Document Envelope 2.0",
  "description": "Format-neutral, lossless-at-the-model-layer envelope for UW Markdown representations.",
  "type": "object",
  "additionalProperties": true,
  "required": [
    "envelope_version",
    "format_version",
    "frontmatter",
    "sections",
    "pipeline_log",
    "custom_calculations",
    "custom_scenarios",
    "extensions",
    "superseded"
  ],
  "properties": {
    "envelope_version": {
      "const": "2.0"
    },
    "format_version": {
      "const": "3.0",
      "description": "Only admitted document format (RFC 0081); envelope_version remains independently versioned."
    },
    "generated_at": {
      "type": "string",
      "format": "date-time"
    },
    "generator": {
      "type": "string",
      "minLength": 1
    },
    "semantic_digest": {
      "type": "string",
      "pattern": "^sha256:[0-9a-f]{64}$"
    },
    "frontmatter": {
      "type": "object",
      "required": [
        "uw_version",
        "deal_id",
        "deal_name"
      ],
      "properties": {
        "uw_version": {
          "const": "3.0",
          "description": "Format 3.0 is the kept rule set; the explicit uw_version agrees with format_version. RFC 0124 requires this declaration in Lite sources too; no representation-version fallback."
        },
        "deal_id": {
          "type": "string"
        },
        "deal_name": {
          "type": "string"
        },
        "asset_class": {
          "type": "string"
        }
      },
      "additionalProperties": true,
      "description": "Carries declared frontmatter, including well-formed source values that validation may reject. Format §2.2 closed vocabularies (RFC 0118) are Verifier checks, not additional Reader transport-admission requirements."
    },
    "sections": {
      "type": "object",
      "additionalProperties": {
        "oneOf": [
          {
            "$ref": "#/$defs/block"
          },
          {
            "$ref": "#/$defs/variantMap"
          }
        ]
      }
    },
    "pipeline_log": {
      "$ref": "#/$defs/blockArray"
    },
    "custom_calculations": {
      "$ref": "#/$defs/blockArray"
    },
    "custom_scenarios": {
      "$ref": "#/$defs/blockArray"
    },
    "extensions": {
      "type": "object",
      "additionalProperties": {
        "$ref": "#/$defs/extension"
      }
    },
    "superseded": {
      "type": "object",
      "additionalProperties": {
        "$ref": "#/$defs/blockArray"
      }
    }
  },
  "$defs": {
    "annotation": {
      "type": "object",
      "required": [
        "section"
      ],
      "properties": {
        "section": {
          "type": "string",
          "minLength": 1
        }
      },
      "additionalProperties": {
        "type": [
          "string",
          "number",
          "boolean"
        ]
      }
    },
    "meta": {
      "$ref": "https://uwmd.org/schemas/uwmd-block.schema.json#/$defs/UWMeta"
    },
    "content": {
      "$ref": "https://uwmd.org/schemas/uwmd-block.schema.json"
    },
    "block": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "annotation",
        "content"
      ],
      "properties": {
        "annotation": {
          "$ref": "#/$defs/annotation"
        },
        "content": {
          "$ref": "#/$defs/content"
        },
        "prose": {
          "type": "string"
        }
      }
    },
    "variantMap": {
      "type": "object",
      "minProperties": 1,
      "additionalProperties": {
        "$ref": "#/$defs/block"
      }
    },
    "blockArray": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/block"
      }
    },
    "extension": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "mode",
            "block"
          ],
          "properties": {
            "mode": {
              "const": "single"
            },
            "block": {
              "$ref": "#/$defs/block"
            }
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": [
            "mode",
            "blocks"
          ],
          "properties": {
            "mode": {
              "const": "variants"
            },
            "blocks": {
              "type": "array",
              "minItems": 1,
              "items": {
                "type": "object",
                "additionalProperties": false,
                "required": [
                  "variant",
                  "block"
                ],
                "properties": {
                  "variant": {
                    "type": [
                      "string",
                      "null"
                    ]
                  },
                  "block": {
                    "$ref": "#/$defs/block"
                  }
                }
              }
            }
          }
        }
      ]
    }
  }
}
