{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://relaynotes.com/spec/relaymark/schemas/relaymark-document.schema.json",
  "title": "RelayMark Document",
  "description": "Canonical RelayMark AST document.",
  "type": "object",
  "additionalProperties": false,
  "required": ["type", "version", "blocks"],
  "properties": {
    "type": {
      "const": "relaymark.document"
    },
    "version": {
      "type": "integer",
      "minimum": 1
    },
    "frontmatter": {
      "type": "object",
      "additionalProperties": true
    },
    "blocks": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/block"
      }
    },
    "annotations": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/annotation"
      },
      "default": []
    },
    "resources": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/resource"
      },
      "default": []
    },
    "meta": {
      "type": "object",
      "additionalProperties": true
    }
  },
  "$defs": {
    "id": {
      "type": "string",
      "minLength": 1,
      "pattern": "^[A-Za-z][A-Za-z0-9_-]*$"
    },
    "blockType": {
      "type": "string",
      "anyOf": [
        {
          "pattern": "^(paragraph|heading|list|list_item|quote|code|table|table_row|table_cell|image|horizontal_rule|widget\\.[a-z][a-z0-9_]*)$"
        },
        {
          "pattern": "^relay\\.[a-z][a-z0-9_]*$",
          "not": {
            "pattern": "_ref$"
          }
        },
        {
          "enum": ["relay.evidence_ref", "relay.file_ref", "relay.note_ref", "relay.url_ref"]
        }
      ]
    },
    "inlineType": {
      "type": "string",
      "anyOf": [
        {
          "enum": ["text", "hard_break", "soft_break", "link", "emphasis", "strong", "code_span"]
        },
        {
          "pattern": "^relay\\.[a-z][a-z0-9_]*_ref$",
          "not": {
            "enum": ["relay.evidence_ref", "relay.file_ref", "relay.note_ref", "relay.url_ref"]
          }
        }
      ]
    },
    "jsonValue": {
      "anyOf": [
        {"type": "null"},
        {"type": "boolean"},
        {"type": "number"},
        {"type": "string"},
        {
          "type": "array",
          "items": {
            "$ref": "#/$defs/jsonValue"
          }
        },
        {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/jsonValue"
          }
        }
      ]
    },
    "block": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "type": {
          "$ref": "#/$defs/blockType"
        },
        "props": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/jsonValue"
          },
          "default": {}
        },
        "content": {
          "type": "array",
          "items": {
            "anyOf": [
              {"$ref": "#/$defs/inline"},
              {"$ref": "#/$defs/block"}
            ]
          },
          "default": []
        },
        "evidence_refs": {
          "type": "array",
          "items": {
            "type": "string",
            "minLength": 1
          },
          "default": []
        },
        "source_location": {
          "$ref": "#/$defs/location"
        },
        "meta": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/jsonValue"
          }
        }
      },
      "oneOf": [
        {
          "required": ["type", "props"],
          "properties": {
            "type": {"const": "relay.source_bundle"},
            "props": {
              "required": ["id"],
              "properties": {
                "id": {"type": "string", "minLength": 1}
              }
            }
          }
        },
        {
          "required": ["type", "props", "content"],
          "properties": {
            "type": {"const": "relay.evidence_ref"},
            "props": {
              "$ref": "#/$defs/scriptureEvidenceProps"
            },
            "content": {
              "type": "array",
              "minItems": 1
            }
          }
        },
        {
          "required": ["type", "props"],
          "properties": {
            "type": {"const": "relay.evidence_ref"},
            "props": {
              "required": ["id", "source"],
              "properties": {
                "id": {"type": "string", "minLength": 1},
                "source": {"type": "string", "minLength": 1}
              },
              "not": {
                "required": ["source_type"],
                "properties": {
                  "source_type": {"const": "scripture"}
                }
              }
            }
          }
        },
        {
          "required": ["type"],
          "properties": {
            "type": {
              "not": {
                "enum": ["relay.source_bundle", "relay.evidence_ref"]
              }
            }
          }
        }
      ]
    },
    "inline": {
      "type": "object",
      "additionalProperties": false,
      "required": ["type"],
      "properties": {
        "type": {
          "$ref": "#/$defs/inlineType"
        },
        "text": {
          "type": "string"
        },
        "id": {
          "type": "string"
        },
        "label": {
          "type": "string"
        },
        "href": {
          "type": "string"
        },
        "props": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/jsonValue"
          }
        },
        "content": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/inline"
          }
        }
      }
    },
    "annotation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "anchor"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "type": {
          "type": "string",
          "pattern": "^(comment|private_note|highlight|agent_suggestion|redaction|source_grounding|review_decision)$"
        },
        "anchor": {
          "$ref": "#/$defs/anchor"
        },
        "props": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/jsonValue"
          }
        },
        "content": {
          "type": "array",
          "items": {
            "$ref": "#/$defs/inline"
          }
        }
      }
    },
    "anchor": {
      "type": "object",
      "additionalProperties": false,
      "required": ["block_id"],
      "properties": {
        "block_id": {
          "$ref": "#/$defs/id"
        },
        "start": {
          "type": "integer",
          "minimum": 0
        },
        "end": {
          "type": "integer",
          "minimum": 0
        },
        "quote": {
          "type": "string"
        }
      }
    },
    "resource": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type"],
      "properties": {
        "id": {
          "$ref": "#/$defs/id"
        },
        "type": {
          "type": "string",
          "pattern": "^(transcript|file|url|image|video|audio|entity|event|note|form_submission|scripture)$"
        },
        "uri": {
          "type": "string"
        },
        "label": {
          "type": "string"
        },
        "scope": {
          "type": "string",
          "enum": ["private", "org", "unlisted", "public"]
        },
        "props": {
          "type": "object",
          "additionalProperties": {
            "$ref": "#/$defs/jsonValue"
          }
        }
      },
      "oneOf": [
        {
          "required": ["type", "scope", "props"],
          "properties": {
            "type": {"const": "scripture"},
            "props": {
              "$ref": "#/$defs/scriptureRelease"
            }
          }
        },
        {
          "properties": {
            "type": {
              "not": {"const": "scripture"}
            }
          }
        }
      ]
    },
    "scriptureRelease": {
      "type": "object",
      "additionalProperties": false,
      "required": [
        "profile",
        "translation_id",
        "edition_id",
        "corpus_release_id",
        "versification_id",
        "source",
        "license",
        "attribution",
        "provenance"
      ],
      "properties": {
        "profile": {"const": "relaymark.scripture_release.v1"},
        "translation_id": {"type": "string", "minLength": 1},
        "edition_id": {"type": "string", "minLength": 1},
        "corpus_release_id": {"type": "string", "minLength": 1},
        "versification_id": {"type": "string", "minLength": 1},
        "source": {"$ref": "#/$defs/scriptureSource"},
        "license": {"$ref": "#/$defs/scriptureLicense"},
        "attribution": {"type": "string", "minLength": 1},
        "provenance": {"$ref": "#/$defs/scriptureProvenance"}
      }
    },
    "scriptureSource": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "uri", "digest"],
      "properties": {
        "name": {"type": "string", "minLength": 1},
        "uri": {"type": "string", "minLength": 1},
        "digest": {"type": "string", "pattern": "^sha256:[0-9a-f]{64}$"}
      }
    },
    "scriptureLicense": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "name", "url"],
      "properties": {
        "id": {"type": "string", "minLength": 1},
        "name": {"type": "string", "minLength": 1},
        "url": {"type": "string", "minLength": 1}
      }
    },
    "scriptureProvenance": {
      "type": "object",
      "additionalProperties": false,
      "required": ["coordinate_inventory_digest", "normalized_index_input_digest"],
      "properties": {
        "coordinate_inventory_digest": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        "normalized_index_input_digest": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        "source_archive_digest": {
          "type": "string",
          "pattern": "^sha256:[0-9a-f]{64}$"
        },
        "authority": {"type": "string", "minLength": 1},
        "imported_at": {"type": "string", "minLength": 1}
      }
    },
    "scriptureCoordinate": {
      "oneOf": [
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["book_id", "coordinate_kind", "chapter", "verse_label"],
          "properties": {
            "book_id": {"type": "string", "minLength": 1},
            "coordinate_kind": {"const": "chapter"},
            "chapter": {"type": "integer", "minimum": 1},
            "verse_label": {"type": "string", "minLength": 1}
          }
        },
        {
          "type": "object",
          "additionalProperties": false,
          "required": ["book_id", "coordinate_kind", "section_id", "verse_label"],
          "properties": {
            "book_id": {"type": "string", "minLength": 1},
            "coordinate_kind": {"const": "section"},
            "section_id": {"type": "string", "minLength": 1},
            "verse_label": {"type": "string", "minLength": 1}
          }
        }
      ]
    },
    "scriptureSpan": {
      "type": "object",
      "additionalProperties": false,
      "required": ["profile", "start", "end", "content_digest"],
      "properties": {
        "profile": {"const": "relaymark.scripture_span.v1"},
        "start": {"$ref": "#/$defs/scriptureCoordinate"},
        "end": {"$ref": "#/$defs/scriptureCoordinate"},
        "content_digest": {"type": "string", "pattern": "^sha256:[0-9a-f]{64}$"},
        "display_label": {"type": "string", "minLength": 1}
      }
    },
    "scriptureEvidenceProps": {
      "type": "object",
      "additionalProperties": true,
      "required": [
        "id",
        "source",
        "source_type",
        "source_revision",
        "selector",
        "scope"
      ],
      "properties": {
        "id": {"type": "string", "minLength": 1},
        "source": {"type": "string", "minLength": 1},
        "source_type": {"const": "scripture"},
        "source_revision": {"type": "string", "minLength": 1},
        "selector": {"$ref": "#/$defs/scriptureSpan"},
        "label": {"type": "string"},
        "scope": {
          "type": "string",
          "enum": ["private", "org", "unlisted", "public"]
        }
      }
    },
    "location": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "line": {
          "type": "integer",
          "minimum": 1
        },
        "column": {
          "type": "integer",
          "minimum": 1
        },
        "end_line": {
          "type": "integer",
          "minimum": 1
        },
        "end_column": {
          "type": "integer",
          "minimum": 1
        }
      }
    }
  }
}
