{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemas.openchronology.org/v0.3/full.schema.json",
  "title": "OpenChronology Full Event Schema",
  "description": "Complete schema for .chron files including all optional blocks. Covers Tier 3 (Full) requirements. Strict superset of core.schema.json.",
  "x-openchronology-version": "0.3",
  "x-openchronology-status": "pre-release",
  "x-openchronology-license": "CC-BY-4.0",

  "type": "object",
  "required": ["meta", "event"],
  "additionalProperties": false,

  "properties": {

    "meta": {
      "type": "object",
      "required": ["schema_version"],
      "additionalProperties": false,
      "properties": {
        "schema_version": { "type": "string", "const": "0.3" },
        "author": {
          "oneOf": [
            { "type": "string" },
            { "type": "array", "items": { "type": "string" }, "minItems": 1 }
          ]
        },
        "license":       { "type": "string" },
        "generator":     { "type": "string" },
        "canonical_url": { "type": "string", "format": "uri" },
        "$schema":       { "type": "string", "format": "uri" }
      }
    },

    "definitions": {
      "type": "object",
      "additionalProperties": false,
      "properties": {

        "universes": {
          "type": "object",
          "additionalProperties": {
            "type": "object",
            "required": ["type"],
            "additionalProperties": false,
            "properties": {
              "type":        { "type": "string", "enum": ["builtin","external","absolute","relative"] },
              "ref":         { "type": "string" },
              "ref_version": { "type": "string" },
              "base_unit":   { "type": "string" },
              "anchor": {
                "type": "object",
                "required": ["target_universe","offset_value","scale_factor"],
                "additionalProperties": false,
                "properties": {
                  "target_universe": { "type": "string" },
                  "offset_value":    { "type": "string" },
                  "scale_factor":    { "type": "number" }
                }
              }
            },
            "if":   { "properties": { "type": { "const": "external" } }, "required": ["type"] },
            "then": { "required": ["ref"] }
          }
        },

        "calendars": {
          "type": "object",
          "additionalProperties": {
            "type": "object",
            "required": ["type"],
            "additionalProperties": false,
            "properties": {
              "type":            { "type": "string", "enum": ["builtin","external"] },
              "ref":             { "type": "string" },
              "ref_version":     { "type": "string" },
              "canonical_url":   { "type": "string", "format": "uri" },
              "inline_fallback": { "type": "object" }
            },
            "if":   { "properties": { "type": { "const": "external" } }, "required": ["type"] },
            "then": { "required": ["ref"] }
          }
        },

        "bodies": {
          "type": "object",
          "description": "Built-in bodies (earth, moon, mars, sun) may use { type: 'builtin' } without a name field.",
          "additionalProperties": {
            "type": "object",
            "required": ["type"],
            "additionalProperties": false,
            "properties": {
              "name":      { "type": "string" },
              "type":      { "type": "string" },
              "crs":       { "type": "string" },
              "image_url": { "type": "string", "format": "uri" },
              "width":     { "type": "integer" },
              "height":    { "type": "integer" }
            },
            "if":   { "properties": { "type": { "not": { "const": "builtin" } } } },
            "then": { "required": ["name","type"] }
          }
        }
      }
    },

    "event": {
      "type": "object",
      "required": ["id","type"],
      "properties": {
        "id":   { "type": "string", "format": "uuid" },
        "type": { "type": "string", "enum": ["event","era","marker","tombstone","deprecated"], "default": "event" },

        "content": {
          "type": "object",
          "required": ["title"],
          "additionalProperties": false,
          "properties": {
            "title":              { "type": "string" },
            "description":        { "type": "string" },
            "description_format": { "type": "string", "enum": ["plain","markdown","html"], "default": "plain" },
            "date_label":         { "type": "string" },
            "tags":               { "type": "array", "items": { "type": "string" } },
            "sources": {
              "type": "array",
              "description": "External citations. Use this for Wikipedia, official pages, academic papers. Do NOT use relations for external URLs.",
              "items": {
                "type": "object",
                "required": ["url","title","type"],
                "additionalProperties": false,
                "properties": {
                  "url":         { "type": "string", "format": "uri" },
                  "title":       { "type": "string" },
                  "type":        { "type": "string", "enum": ["wikipedia","official","academic","archive","news","reference","custom"] },
                  "description": { "type": "string" }
                }
              }
            },
            "translations": {
              "type": "object",
              "additionalProperties": {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                  "title":              { "type": "string" },
                  "description":        { "type": "string" },
                  "date_label":         { "type": "string" },
                  "description_format": { "type": "string", "enum": ["plain","markdown","html"] }
                }
              }
            }
          }
        },

        "temporal": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "universe":    { "type": "string" },
            "calendar":    { "type": "string" },
            "start_value": { "type": "string" },
            "end_value":   { "type": "string" },
            "precision": {
              "type": "string",
              "enum": ["millisecond","second","minute","hour","day","month","year","decade","century","millennium","epoch","circa","custom"]
            },
            "format_hint": {
              "type": "object",
              "additionalProperties": false,
              "properties": {
                "pattern":   { "type": "string" },
                "era_label": { "type": ["string","null"] }
              }
            },
            "uncertainty": {
              "type": "object",
              "required": ["type","margin_value","margin_unit"],
              "additionalProperties": false,
              "properties": {
                "type":         { "type": "string", "enum": ["range","before","after","no_earlier_than","no_later_than"] },
                "margin_value": { "type": "number" },
                "margin_unit":  { "type": "string" },
                "confidence":   { "type": ["number","null"], "minimum": 0, "maximum": 1 }
              }
            }
          }
        },

        "spatial": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "body":          { "type": "string" },
            "crs":           { "type": "string" },
            "region_codes":  { "type": "array", "items": { "type": "string" } },
            "custom_region": { "type": "string" },
            "geometry": {
              "type": "object",
              "required": ["type","coordinates"],
              "additionalProperties": false,
              "properties": {
                "type":          { "type": "string", "enum": ["Point","Polygon","LineString"] },
                "coordinates":   { "type": "array" },
                "radius_meters": { "type": "number" },
                "label":         { "type": "string" }
              }
            }
          }
        },

        "media": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["type","url"],
            "additionalProperties": false,
            "properties": {
              "type":    { "type": "string", "enum": ["video","audio","image","document"] },
              "url":     { "type": "string" },
              "alt":     { "type": "string" },
              "caption": { "type": "string" },
              "credit":  { "type": "string" },
              "sync": {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                  "anchor_value":   { "type": "string" },
                  "offset_seconds": { "type": "number" }
                }
              }
            }
          }
        },

        "custom_data": { "type": "object" },

        "relations": {
          "type": "array",
          "description": "Inter-.chron event links only. target_id is required. Use content.sources for external URLs.",
          "items": {
            "type": "object",
            "required": ["type","target_id"],
            "additionalProperties": false,
            "properties": {
              "type": {
                "type": "string",
                "enum": [
                  "parent","child",
                  "precedes","follows","concurrent_with",
                  "causes","caused_by",
                  "influences","influenced_by",
                  "contains","contained_by",
                  "contradicts",
                  "retcons","retconned_by",
                  "variant_of",
                  "references","referenced_by"
                ]
              },
              "target_id":     { "type": "string", "format": "uuid" },
              "target_url":    { "type": "string" },
              "description":   { "type": "string" },
              "bidirectional": { "type": "boolean", "default": false },
              "confidence":    { "type": ["number","null"], "minimum": 0, "maximum": 1 },
              "canon": {
                "oneOf": [
                  { "$ref": "#/$defs/canon_object" },
                  { "type": "array", "items": { "$ref": "#/$defs/canon_object" }, "minItems": 1 }
                ]
              }
            }
          }
        },

        "significance": {
          "type": "object",
          "additionalProperties": false,
          "properties": {
            "value": { "type": "number", "minimum": 0, "maximum": 1000 },
            "temporal_scope": {
              "type": "string",
              "enum": ["epochal","millennial","centennial","generational","decadal","annual","seasonal","monthly","weekly","daily","hourly"]
            },
            "geographic_scope": {
              "type": "string",
              "enum": ["global","continental","national","regional","local","street"]
            },
            "metrics": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["type","value"],
                "additionalProperties": false,
                "properties": {
                  "type": {
                    "type": "string",
                    "enum": [
                      "political","military","economic","cultural","scientific","technological",
                      "environmental","ecological","demographic","social","medical","religious",
                      "personal","narrative","custom"
                    ]
                  },
                  "value":       { "type": "number", "minimum": 0, "maximum": 1000 },
                  "description": { "type": "string" }
                }
              }
            },
            "visibility": {
              "type": "object",
              "additionalProperties": false,
              "properties": {
                "min_temporal_zoom":   { "type": "string" },
                "max_temporal_zoom":   { "type": "string" },
                "min_geographic_zoom": { "type": "string" },
                "max_geographic_zoom": { "type": "string" }
              }
            }
          }
        },

        "recurrence": {
          "type": "object",
          "required": ["enabled","frequency"],
          "additionalProperties": false,
          "properties": {
            "enabled": { "type": "boolean" },
            "frequency": {
              "type": "object",
              "required": ["unit","interval"],
              "additionalProperties": false,
              "properties": {
                "unit":     { "type": "string", "enum": ["day","week","month","year","custom"] },
                "interval": { "type": "integer", "minimum": 1 }
              }
            },
            "on": {
              "type": "object",
              "additionalProperties": false,
              "properties": {
                "month": { "type": "string" },
                "day":   { "type": "integer", "minimum": 1 }
              }
            },
            "end": {
              "type": "object",
              "required": ["type"],
              "additionalProperties": false,
              "properties": {
                "type":  { "type": "string", "enum": ["count","until","none"] },
                "count": { "type": "integer", "minimum": 1 },
                "until": { "type": "string" }
              }
            },
            "exceptions": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["instance_value","type"],
                "additionalProperties": false,
                "properties": {
                  "instance_value": { "type": "string" },
                  "type":           { "type": "string", "enum": ["cancelled","modified"] },
                  "overrides":      { "type": "object" }
                }
              }
            }
          }
        },

        "dynamic": {
          "type": "object",
          "required": ["enabled"],
          "additionalProperties": false,
          "properties": {
            "enabled": { "type": "boolean" },
            "formulas": {
              "type": "array",
              "items": {
                "type": "object",
                "required": ["target_field","type","expression"],
                "additionalProperties": false,
                "properties": {
                  "target_field": { "type": "string" },
                  "type":         { "type": "string", "enum": ["interpolation","formula"] },
                  "expression":   { "type": "string" },
                  "dataset":      { "type": "array" },
                  "dataset_ref": {
                    "type": "object",
                    "required": ["type"],
                    "additionalProperties": false,
                    "properties": {
                      "type":   { "type": "string", "enum": ["url","file"] },
                      "url":    { "type": "string", "format": "uri" },
                      "path":   { "type": "string" },
                      "format": { "type": "string", "const": "openchronology_dataset" }
                    }
                  }
                }
              }
            }
          }
        },

        "tombstone": {
          "type": "object",
          "required": ["original_type","reason"],
          "additionalProperties": false,
          "properties": {
            "original_type": { "type": "string", "enum": ["event","era","marker"] },
            "reason":        { "type": "string" },
            "deleted_by":    { "type": "string" },
            "deleted_at":    { "type": "string", "format": "date-time" },
            "superseded_by": { "type": "string", "format": "uuid" }
          }
        },

        "deprecated": {
          "type": "object",
          "required": ["original_type","reason"],
          "additionalProperties": false,
          "properties": {
            "original_type": { "type": "string", "enum": ["event","era","marker"] },
            "reason":        { "type": "string" },
            "deprecated_by": { "type": "string" },
            "deprecated_at": { "type": "string", "format": "date-time" },
            "superseded_by": { "type": "string", "format": "uuid" },
            "display": { "type": "string", "enum": ["visible","distinguished","hidden"], "default": "distinguished" }
          }
        }
      },

      "if":   { "properties": { "type": { "const": "tombstone" } }, "required": ["type"] },
      "then": {
        "required": ["tombstone"],
        "not": {
          "anyOf": [
            { "required": ["content"] }, { "required": ["temporal"] }, { "required": ["spatial"] },
            { "required": ["relations"] }, { "required": ["recurrence"] }, { "required": ["significance"] },
            { "required": ["media"] }, { "required": ["custom_data"] }, { "required": ["dynamic"] }
          ]
        }
      },
      "else": {
        "if":   { "properties": { "type": { "const": "deprecated" } }, "required": ["type"] },
        "then": { "required": ["deprecated","content"] },
        "else": { "required": ["content"] }
      }
    }
  },

  "$defs": {
    "canon_object": {
      "type": "object",
      "required": ["scope","status"],
      "additionalProperties": false,
      "properties": {
        "scope":  { "type": "string" },
        "status": { "type": "string", "enum": ["canon","legends","disputed","non_canon","unknown"] }
      }
    }
  }
}
