{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://analysisreports.github.io/schemas/graph.schema.json",
  "title": "Federated Knowledge Graph Schema",
  "description": "Structural contract for the master graph router and modular graph documents.",
  "oneOf": [
    { "$ref": "#/$defs/masterGraph" },
    { "$ref": "#/$defs/modularSubgraph" }
  ],
  "$defs": {
    "schemaReference": {
      "const": "https://analysisreports.github.io/schemas/graph.schema.json"
    },
    "uri": {
      "type": "string",
      "format": "uri"
    },
    "timestamp": {
      "type": "string",
      "format": "date-time"
    },
    "nodeCategory": {
      "type": "string",
      "enum": [
        "ACTOR",
        "CHOKEPOINT",
        "INDICATOR",
        "MODEL",
        "ENDPOINT_SCHEMA",
        "COMMODITY_STRATEGIC",
        "CRITICAL_MINERAL",
        "ENERGY_BENCHMARK"
      ]
    },
    "edgeType": {
      "type": "string",
      "enum": [
        "BRIDGES_TO_MODEL",
        "CONTROLS_LEVERAGE",
        "CONTROLS_TERRITORY",
        "EXECUTES_MODEL",
        "FLANKS_CHOKEPOINT",
        "INCLUDES_MEMBER",
        "INDEXES",
        "INFLATION_IMPACT",
        "INGESTS_SERIES",
        "MONETARY_AUTHORITY",
        "PATRON_COUPLING",
        "POLARITY_COUPLING",
        "PROXIMATE_TO",
        "REGULATES_TRANSIT",
        "SETS_POLICY_RATE",
        "SUPPLY_DISRUPTION",
        "TERMINATES_AT",
        "TRANSMITS_RISK",
        "TRIGGERS_INDICATOR"
      ]
    },
    "node": {
      "type": "object",
      "required": ["id", "label", "category"],
      "properties": {
        "id": {
          "type": "string",
          "pattern": "^[A-Z][A-Z0-9_.-]*$"
        },
        "label": {
          "type": "string",
          "minLength": 1
        },
        "category": { "$ref": "#/$defs/nodeCategory" },
        "type": { "type": "string" },
        "attributes": {
          "type": "object",
          "not": {
            "required": ["members"]
          },
          "additionalProperties": true
        }
      },
      "additionalProperties": true
    },
    "edge": {
      "type": "object",
      "required": ["source", "target", "type"],
      "properties": {
        "source": {
          "type": "string",
          "pattern": "^[A-Z][A-Z0-9_.-]*$"
        },
        "target": {
          "type": "string",
          "pattern": "^[A-Z][A-Z0-9_.-]*$"
        },
        "type": { "$ref": "#/$defs/edgeType" },
        "p_baseline": {
          "type": ["number", "null"],
          "minimum": -1,
          "maximum": 1
        }
      },
      "additionalProperties": true
    },
    "metadata": {
      "type": "object",
      "required": [
        "title",
        "domain",
        "version",
        "author",
        "license",
        "endpoint",
        "documentation",
        "last_updated"
      ],
      "properties": {
        "title": { "type": "string", "minLength": 1 },
        "domain": { "const": "graph" },
        "version": { "type": "string", "minLength": 1 },
        "author": { "type": "string", "minLength": 1 },
        "license": { "type": "string", "minLength": 1 },
        "endpoint": { "$ref": "#/$defs/uri" },
        "documentation": { "$ref": "#/$defs/uri" },
        "last_updated": { "$ref": "#/$defs/timestamp" },
        "master_graph": { "$ref": "#/$defs/uri" },
        "subgraph_domain": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": true
    },
    "count": {
      "type": "integer",
      "minimum": 0
    },
    "subgraphRecord": {
      "type": "object",
      "required": ["nodes_count", "edges_count", "scope"],
      "properties": {
        "endpoint": { "$ref": "#/$defs/uri" },
        "nodes_count": { "$ref": "#/$defs/count" },
        "edges_count": { "$ref": "#/$defs/count" },
        "token_size": { "type": "string" },
        "node_prefixes": {
          "type": "array",
          "items": { "type": "string", "minLength": 1 },
          "uniqueItems": true
        },
        "edge_types": {
          "type": "array",
          "items": { "$ref": "#/$defs/edgeType" },
          "uniqueItems": true
        },
        "scope": { "type": "string", "minLength": 1 }
      },
      "additionalProperties": false
    },
    "masterGraph": {
      "type": "object",
      "required": [
        "$schema",
        "name",
        "version",
        "author",
        "license",
        "repository",
        "architecture_type",
        "routing_policy",
        "graph_accounting",
        "subgraphs",
        "graph_metadata",
        "nodes",
        "edges",
        "metadata"
      ],
      "properties": {
        "$schema": { "$ref": "#/$defs/schemaReference" },
        "name": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 },
        "author": { "type": "string", "minLength": 1 },
        "license": { "type": "string", "minLength": 1 },
        "repository": { "$ref": "#/$defs/uri" },
        "architecture_type": { "const": "FEDERATED_MODULAR_GRAPH" },
        "routing_policy": {
          "type": "object",
          "required": [
            "routing_instruction",
            "chokepoints_subgraph",
            "models_subgraph",
            "actors_subgraph",
            "blocs_subgraph"
          ],
          "properties": {
            "routing_instruction": { "type": "string", "minLength": 1 },
            "chokepoints_subgraph": { "$ref": "#/$defs/uri" },
            "models_subgraph": { "$ref": "#/$defs/uri" },
            "actors_subgraph": { "$ref": "#/$defs/uri" },
            "blocs_subgraph": { "$ref": "#/$defs/uri" }
          },
          "additionalProperties": false
        },
        "graph_accounting": {
          "type": "object",
          "required": [
            "modular_subgraphs_nodes",
            "modular_subgraphs_edges",
            "core_backbone_nodes",
            "core_backbone_edges",
            "total_federated_nodes",
            "total_federated_edges"
          ],
          "properties": {
            "modular_subgraphs_nodes": { "$ref": "#/$defs/count" },
            "modular_subgraphs_edges": { "$ref": "#/$defs/count" },
            "core_backbone_nodes": { "$ref": "#/$defs/count" },
            "core_backbone_edges": { "$ref": "#/$defs/count" },
            "total_federated_nodes": { "$ref": "#/$defs/count" },
            "total_federated_edges": { "$ref": "#/$defs/count" }
          },
          "additionalProperties": false
        },
        "subgraphs": {
          "type": "object",
          "required": ["chokepoints", "models", "actors", "blocs", "core_backbone"],
          "properties": {
            "chokepoints": { "$ref": "#/$defs/subgraphRecord" },
            "models": { "$ref": "#/$defs/subgraphRecord" },
            "actors": { "$ref": "#/$defs/subgraphRecord" },
            "blocs": { "$ref": "#/$defs/subgraphRecord" },
            "core_backbone": { "$ref": "#/$defs/subgraphRecord" }
          },
          "additionalProperties": false
        },
        "graph_metadata": {
          "type": "object",
          "required": ["node_categories", "edge_types"],
          "properties": {
            "node_categories": {
              "type": "array",
              "items": { "$ref": "#/$defs/nodeCategory" },
              "uniqueItems": true
            },
            "edge_types": {
              "type": "array",
              "items": { "$ref": "#/$defs/edgeType" },
              "uniqueItems": true
            }
          },
          "additionalProperties": false
        },
        "nodes": {
          "type": "array",
          "items": { "$ref": "#/$defs/node" },
          "uniqueItems": true
        },
        "edges": {
          "type": "array",
          "items": { "$ref": "#/$defs/edge" },
          "uniqueItems": true
        },
        "metadata": { "$ref": "#/$defs/metadata" }
      },
      "additionalProperties": false
    },
    "modularSubgraph": {
      "type": "object",
      "required": [
        "$schema",
        "name",
        "version",
        "author",
        "license",
        "repository",
        "endpoint",
        "master_graph",
        "subgraph_domain",
        "last_updated",
        "nodes",
        "edges",
        "metadata"
      ],
      "properties": {
        "$schema": { "$ref": "#/$defs/schemaReference" },
        "name": { "type": "string", "minLength": 1 },
        "version": { "type": "string", "minLength": 1 },
        "author": { "type": "string", "minLength": 1 },
        "license": { "type": "string", "minLength": 1 },
        "repository": { "$ref": "#/$defs/uri" },
        "endpoint": { "$ref": "#/$defs/uri" },
        "master_graph": { "$ref": "#/$defs/uri" },
        "subgraph_domain": { "type": "string", "minLength": 1 },
        "last_updated": { "$ref": "#/$defs/timestamp" },
        "nodes": {
          "type": "array",
          "items": { "$ref": "#/$defs/node" },
          "uniqueItems": true
        },
        "edges": {
          "type": "array",
          "items": { "$ref": "#/$defs/edge" },
          "uniqueItems": true
        },
        "metadata": { "$ref": "#/$defs/metadata" }
      },
      "additionalProperties": false
    }
  }
}
