{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Chronicle9 exchange v1",
  "type": "object",
  "additionalProperties": false,
  "required": ["format", "version", "contexts", "records", "relationships"],
  "properties": {
    "format": {"const": "chronicle9.exchange"},
    "version": {"const": 1},
    "contexts": {"type": "array", "items": {"$ref": "#/$defs/context"}},
    "records": {"type": "array", "items": {"$ref": "#/$defs/record"}},
    "relationships": {"type": "array", "items": {"$ref": "#/$defs/relationship"}}
  },
  "anyOf": [
    {"properties": {"contexts": {"minItems": 1}}},
    {"properties": {"records": {"minItems": 1}}}
  ],
  "$defs": {
    "id": {"type": "string", "minLength": 1, "maxLength": 128},
    "metadata": {"type": ["object", "array"], "description": "Provenance, reasoning, chronology, uncertainty and extension data. Empty PHP objects may serialize as arrays."},
    "date": {"type": ["string", "null"], "pattern": "^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}$"},
    "context": {
      "type": "object", "additionalProperties": false,
      "required": ["id", "purpose", "title"],
      "properties": {
        "id": {"$ref": "#/$defs/id"},
        "purpose": {"enum": ["project", "memory", "story"]},
        "title": {"type": "string", "minLength": 1, "maxLength": 160},
        "description": {"type": "string", "maxLength": 1000000},
        "status": {"enum": ["active", "archived"]},
        "metadata": {"$ref": "#/$defs/metadata"}
      }
    },
    "record": {
      "type": "object", "additionalProperties": false,
      "required": ["id", "type", "title"],
      "properties": {
        "id": {"$ref": "#/$defs/id"},
        "type": {"type": "string", "pattern": "^[a-z][a-z0-9_]{0,59}$"},
        "title": {"type": "string", "minLength": 1, "maxLength": 255},
        "content": {"type": "string", "maxLength": 10000000},
        "event_date": {"$ref": "#/$defs/date"},
        "location": {"type": "string", "maxLength": 255},
        "state": {"enum": ["active", "current", "historical", "superseded", "draft", "canon", "discarded", "uncertain", "archived"]},
        "metadata": {"$ref": "#/$defs/metadata"},
        "contexts": {"type": "array", "uniqueItems": true, "items": {"$ref": "#/$defs/id"}},
        "versions": {"type": "array", "items": {"$ref": "#/$defs/version"}},
        "attachments": {"type": "array", "items": {"$ref": "#/$defs/attachment"}}
      }
    },
    "version": {
      "type": "object", "additionalProperties": false, "required": ["number", "snapshot"],
      "properties": {
        "number": {"type": "integer", "minimum": 1},
        "snapshot": {"$ref": "#/$defs/metadata"},
        "recorded_at": {"$ref": "#/$defs/date"}
      }
    },
    "attachment": {
      "type": "object", "additionalProperties": false,
      "required": ["id", "name", "mime_type", "size_bytes", "availability"],
      "properties": {
        "id": {"$ref": "#/$defs/id"},
        "name": {"type": "string", "minLength": 1, "maxLength": 255},
        "mime_type": {"type": "string", "minLength": 1, "maxLength": 120},
        "size_bytes": {"type": "integer", "minimum": 0},
        "sha256": {"type": ["string", "null"], "pattern": "^[a-f0-9]{64}$"},
        "transcript": {"type": ["string", "null"]},
        "metadata": {"$ref": "#/$defs/metadata"},
        "availability": {"const": "reference_only"}
      }
    },
    "relationship": {
      "type": "object", "additionalProperties": false, "required": ["from", "to", "type"],
      "properties": {
        "from": {"$ref": "#/$defs/id"}, "to": {"$ref": "#/$defs/id"},
        "type": {"type": "string", "minLength": 1, "maxLength": 60},
        "metadata": {"$ref": "#/$defs/metadata"}
      }
    }
  }
}
