{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://analysisreports.github.io/schemas/country-relationships.schema.json",
  "title": "Canonical Sovereign Country Relationships",
  "description": "Contract for the master bilateral router and its four sovereign hub shelves.",
  "oneOf": [
    {
      "$ref": "#/$defs/masterDocument"
    },
    {
      "$ref": "#/$defs/shelfDocument"
    }
  ],
  "$defs": {
    "canonicalSchemaReference": {
      "const": "https://analysisreports.github.io/schemas/country-relationships.schema.json"
    },
    "entityId": {
      "type": "string",
      "pattern": "^(INST_|GOV_|GEO_|REG_|BLOC_|ORG_|CHOKE_|CORRIDOR_|BASIN_|ZONE_)[A-Z0-9_]+$"
    },
    "hubId": {
      "type": "string",
      "enum": ["GEO_US", "GEO_RU", "GEO_CN", "GEO_IR"]
    },
    "url": {
      "type": "string",
      "format": "uri"
    },
    "masterMetadata": {
      "type": "object",
      "required": [
        "title",
        "domain",
        "version",
        "author",
        "license",
        "endpoint",
        "documentation",
        "last_updated",
        "last_successful_sync",
        "architecture",
        "total_hubs",
        "total_dyads",
        "entity_dictionary",
        "multilateral_relationships",
        "canonical_endpoint_txt",
        "canonical_endpoint_json"
      ],
      "properties": {
        "title": {"type": "string", "minLength": 1},
        "domain": {"const": "country"},
        "version": {"type": "string", "minLength": 1},
        "author": {"type": "string", "minLength": 1},
        "license": {"type": "string", "minLength": 1},
        "endpoint": {"$ref": "#/$defs/url"},
        "documentation": {"$ref": "#/$defs/url"},
        "last_updated": {"type": "string", "format": "date-time"},
        "last_successful_sync": {"type": "string", "format": "date-time"},
        "architecture": {"type": "string", "minLength": 1},
        "total_hubs": {"const": 4},
        "total_dyads": {"const": 168},
        "entity_dictionary": {"$ref": "#/$defs/url"},
        "multilateral_relationships": {"$ref": "#/$defs/url"},
        "canonical_endpoint_txt": {"$ref": "#/$defs/url"},
        "canonical_endpoint_json": {"$ref": "#/$defs/url"}
      },
      "additionalProperties": false
    },
    "shelfMetadata": {
      "type": "object",
      "required": [
        "title",
        "domain",
        "version",
        "author",
        "license",
        "endpoint",
        "documentation",
        "last_updated",
        "last_successful_sync",
        "hub_id",
        "hub_name",
        "description",
        "total_dyads",
        "entity_dictionary",
        "master_router_txt",
        "master_router_json",
        "shelf_endpoint_txt",
        "shelf_endpoint_json"
      ],
      "properties": {
        "title": {"type": "string", "minLength": 1},
        "domain": {"const": "country"},
        "version": {"type": "string", "minLength": 1},
        "author": {"type": "string", "minLength": 1},
        "license": {"type": "string", "minLength": 1},
        "endpoint": {"$ref": "#/$defs/url"},
        "documentation": {"$ref": "#/$defs/url"},
        "last_updated": {"type": "string", "format": "date-time"},
        "last_successful_sync": {"type": "string", "format": "date-time"},
        "hub_id": {"$ref": "#/$defs/hubId"},
        "hub_name": {"type": "string", "minLength": 1},
        "description": {"type": "string", "minLength": 1},
        "total_dyads": {"type": "integer", "minimum": 1},
        "entity_dictionary": {"$ref": "#/$defs/url"},
        "master_router_txt": {"$ref": "#/$defs/url"},
        "master_router_json": {"$ref": "#/$defs/url"},
        "shelf_endpoint_txt": {"$ref": "#/$defs/url"},
        "shelf_endpoint_json": {"$ref": "#/$defs/url"}
      },
      "additionalProperties": false
    },
    "masterDyad": {
      "type": "object",
      "required": ["pair", "common_reference", "p_baseline", "classification", "category", "shelf_endpoint"],
      "properties": {
        "pair": {
          "type": "string",
          "pattern": "^GEO_[A-Z0-9_]+ ↔ GEO_[A-Z0-9_]+$"
        },
        "common_reference": {"type": "string", "minLength": 1},
        "p_baseline": {"type": "number", "minimum": -1, "maximum": 1},
        "classification": {"type": "string", "minLength": 1},
        "category": {"type": "string", "minLength": 1},
        "shelf_endpoint": {"$ref": "#/$defs/url"}
      },
      "additionalProperties": false
    },
    "powerVector": {
      "type": "object",
      "required": ["leverage_holder", "domain", "description"],
      "properties": {
        "leverage_holder": {
          "type": "string",
          "anyOf": [
            {"pattern": "^GEO_[A-Z0-9_]+$"},
            {"const": "Mutual/Structural"}
          ]
        },
        "domain": {"type": "string", "minLength": 1},
        "description": {"type": "string", "minLength": 1}
      },
      "additionalProperties": false
    },
    "shelfDyad": {
      "type": "object",
      "required": [
        "dyad_id",
        "actor_1",
        "actor_2",
        "common_reference",
        "bilateral_stance",
        "stance_classification",
        "strategic_category",
        "asymmetric_power_vectors",
        "core_strategic_flashpoints",
        "multilateral_cross_references"
      ],
      "properties": {
        "dyad_id": {"type": "string", "pattern": "^REL_GEO_[A-Z0-9_]+_GEO_[A-Z0-9_]+$"},
        "actor_1": {"type": "string", "pattern": "^GEO_[A-Z0-9_]+$"},
        "actor_2": {"type": "string", "pattern": "^GEO_[A-Z0-9_]+$"},
        "common_reference": {"type": "string", "minLength": 1},
        "bilateral_stance": {"type": "number", "minimum": -1, "maximum": 1},
        "stance_classification": {"type": "string", "minLength": 1},
        "strategic_category": {"type": "string", "minLength": 1},
        "asymmetric_power_vectors": {
          "type": "array",
          "minItems": 2,
          "items": {"$ref": "#/$defs/powerVector"}
        },
        "core_strategic_flashpoints": {
          "type": "array",
          "minItems": 1,
          "items": {"type": "string", "minLength": 1}
        },
        "multilateral_cross_references": {
          "type": "array",
          "minItems": 1,
          "items": {"$ref": "#/$defs/entityId"}
        }
      },
      "additionalProperties": false
    },
    "masterDocument": {
      "type": "object",
      "required": ["$schema", "metadata", "sovereign_shelves_catalog", "sovereign_hubs"],
      "properties": {
        "$schema": {"$ref": "#/$defs/canonicalSchemaReference"},
        "metadata": {"$ref": "#/$defs/masterMetadata"},
        "sovereign_shelves_catalog": {
          "type": "array",
          "minItems": 4,
          "maxItems": 4,
          "items": {
            "type": "object",
            "required": ["hub_id", "hub_name", "slug", "total_dyads", "scope", "endpoint_txt", "endpoint_json"],
            "properties": {
              "hub_id": {"$ref": "#/$defs/hubId"},
              "hub_name": {"type": "string", "minLength": 1},
              "slug": {"type": "string", "enum": ["us", "ru", "cn", "ir"]},
              "total_dyads": {"type": "integer", "minimum": 1},
              "scope": {"type": "string", "minLength": 1},
              "endpoint_txt": {"$ref": "#/$defs/url"},
              "endpoint_json": {"$ref": "#/$defs/url"}
            },
            "additionalProperties": false
          }
        },
        "sovereign_hubs": {
          "type": "object",
          "required": ["GEO_US", "GEO_RU", "GEO_CN", "GEO_IR"],
          "propertyNames": {"$ref": "#/$defs/hubId"},
          "additionalProperties": {
            "type": "object",
            "required": ["hub_id", "hub_name", "shelf_ref_txt", "shelf_ref_json", "total_dyads", "dyads_index"],
            "properties": {
              "hub_id": {"$ref": "#/$defs/hubId"},
              "hub_name": {"type": "string", "minLength": 1},
              "shelf_ref_txt": {"$ref": "#/$defs/url"},
              "shelf_ref_json": {"$ref": "#/$defs/url"},
              "total_dyads": {"type": "integer", "minimum": 1},
              "dyads_index": {
                "type": "object",
                "minProperties": 1,
                "propertyNames": {"pattern": "^REL_GEO_[A-Z0-9_]+_GEO_[A-Z0-9_]+$"},
                "additionalProperties": {"$ref": "#/$defs/masterDyad"}
              }
            },
            "additionalProperties": false
          }
        }
      },
      "additionalProperties": false
    },
    "shelfDocument": {
      "type": "object",
      "required": ["$schema", "metadata", "sovereign_hub"],
      "properties": {
        "$schema": {"$ref": "#/$defs/canonicalSchemaReference"},
        "metadata": {"$ref": "#/$defs/shelfMetadata"},
        "sovereign_hub": {
          "type": "object",
          "required": ["hub_id", "hub_name", "dyads"],
          "properties": {
            "hub_id": {"$ref": "#/$defs/hubId"},
            "hub_name": {"type": "string", "minLength": 1},
            "dyads": {
              "type": "object",
              "minProperties": 1,
              "propertyNames": {"pattern": "^REL_GEO_[A-Z0-9_]+_GEO_[A-Z0-9_]+$"},
              "additionalProperties": {"$ref": "#/$defs/shelfDyad"}
            }
          },
          "additionalProperties": false
        }
      },
      "additionalProperties": false
    }
  }
}
