{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$defs": {
    "citation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source_url", "anchor_quote"],
      "properties": {
        "source_url": { "type": "string", "pattern": "^https://" },
        "anchor_quote": { "type": "string" }
      }
    },
    "facet_map": {
      "type": ["object", "null"],
      "propertyNames": { "pattern": "^[a-zA-Z0-9_.-]+$" },
      "additionalProperties": { "type": ["string", "number", "boolean", "null", "array"] }
    },
    "registration_posture": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode"],
      "properties": {
        "mode": { "type": "string", "enum": ["none", "iana"] }
      }
    },
    "source_link": {
      "type": "object",
      "additionalProperties": false,
      "required": ["label", "url"],
      "properties": {
        "label": { "type": "string" },
        "url": { "type": "string", "pattern": "^https://" }
      }
    },
    "instance_ref": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "org_id", "name", "url", "type", "support_level"],
      "properties": {
        "id": { "type": "string" },
        "org_id": { "type": "string" },
        "name": { "type": "string" },
        "url": { "type": "string", "pattern": "^https://" },
        "type": { "type": "string" },
        "support_level": { "type": "string", "enum": ["full", "partial", "planned", "none"] }
      }
    },
    "probe_expect": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "http_status": { "type": "array", "items": { "type": "integer" } },
        "content_type_prefix": { "type": "string" }
      }
    },
    "probe": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode", "probe_url", "probe_status", "checked_on", "as_of"],
      "properties": {
        "mode": { "type": "string", "enum": ["doc_citation", "http_probe"] },
        "probe_url": { "type": "string", "pattern": "^https://" },
        "probe_status": { "type": "string" },
        "checked_on": { "type": "string", "pattern": "^\\d{4}(?:-\\d{2}(?:-\\d{2})?)?$" },
        "as_of": { "type": ["string", "null"], "pattern": "^\\d{4}(?:-\\d{2}(?:-\\d{2})?)?$" },
        "telemetry": {
          "type": ["object", "null"],
          "propertyNames": { "pattern": "^[a-zA-Z0-9_.-]+$" },
          "additionalProperties": { "type": ["string", "number", "boolean", "null", "array"] }
        },
        "expect": { "$ref": "#/$defs/probe_expect" }
      }
    },
    "row": {
      "type": "object",
      "additionalProperties": false,
      "required": ["actor", "role", "status", "col4_md", "source_links", "citations", "instance", "probe"],
      "properties": {
        "actor": { "type": "string" },
        "role": { "type": "string" },
        "status": { "type": "string" },
        "col4_md": { "type": "string" },
        "full_detail_md": { "type": "string" },
        "facets": { "$ref": "#/$defs/facet_map" },
        "source_links": { "type": "array", "items": { "$ref": "#/$defs/source_link" } },
        "citations": { "type": "array", "items": { "$ref": "#/$defs/citation" } },
        "instance": { "$ref": "#/$defs/instance_ref" },
        "probe": { "$ref": "#/$defs/probe" }
      }
    },
    "table": {
      "type": "object",
      "additionalProperties": false,
      "required": ["heading", "col4_header", "rows"],
      "properties": {
        "heading": { "type": "string" },
        "col4_header": { "type": "string" },
        "id": { "type": "string" },
        "visible": { "type": "boolean" },
        "note_md": { "type": "string" },
        "note_citations": { "type": "array", "items": { "$ref": "#/$defs/citation" } },
        "rows": { "type": "array", "items": { "$ref": "#/$defs/row" } }
      }
    },
    "census": {
      "type": "object",
      "additionalProperties": false,
      "required": ["as_of", "metric", "value_md", "citations"],
      "properties": {
        "as_of": { "type": ["string", "null"], "pattern": "^\\d{4}(?:-\\d{2}(?:-\\d{2})?)?$" },
        "metric": { "type": "string" },
        "value_md": { "type": "string" },
        "citations": { "type": "array", "items": { "$ref": "#/$defs/citation" } },
        "source_label": { "type": "string" },
        "source_url": { "type": "string", "pattern": "^https://" },
        "methodology_note": { "type": "string" },
        "id": { "type": "string" },
        "population": { "type": "string" },
        "anchor_quote": { "type": "string" },
        "numerator": { "type": "integer" },
        "caveat_md": { "type": "string" },
        "denominator": { "type": "integer" },
        "rate": { "type": "number" }
      }
    },
    "footer_clause": {
      "type": "object",
      "additionalProperties": false,
      "required": ["text_md", "citations"],
      "properties": {
        "text_md": { "type": "string" },
        "citations": { "type": "array", "items": { "$ref": "#/$defs/citation" } }
      }
    }
  },
  "type": "object",
  "required": ["version", "archetype_catalog", "protocols"],
  "additionalProperties": false,
  "properties": {
    "version": {
      "type": "string",
      "const": "0.5.0"
    },
    "archetype_catalog": {
      "type": "array",
      "minItems": 6,
      "maxItems": 6,
      "items": {
        "type": "object",
        "required": [
          "schema_id",
          "slug",
          "label",
          "side_a_role",
          "side_b_role",
          "default_col4_header",
          "reference_templates",
          "telemetry_fields"
        ],
        "additionalProperties": false,
        "properties": {
          "schema_id": {
            "type": "string",
            "enum": [
              "db4.payments_onchain.1",
              "db4.web_discovery.1",
              "db4.crawler_policy.1",
              "db4.harnesses_rpc_ui.1",
              "db4.browser_telemetry.1",
              "db4.identity_auth_crypto.1"
            ]
          },
          "slug": {
            "type": "string",
            "enum": [
              "payments_onchain",
              "web_discovery",
              "crawler_policy",
              "harnesses_rpc_ui",
              "browser_telemetry",
              "identity_auth_crypto"
            ]
          },
          "label": { "type": "string", "minLength": 3 },
          "side_a_role": { "type": "string", "minLength": 3 },
          "side_b_role": { "type": "string", "minLength": 3 },
          "default_col4_header": { "type": "string", "minLength": 2 },
          "reference_templates": {
            "type": "array",
            "minItems": 1,
            "items": { "type": "string" }
          },
          "telemetry_fields": {
            "type": "array",
            "minItems": 1,
            "items": { "type": "string" }
          }
        }
      }
    },
    "protocols": {
      "type": "object",
      "additionalProperties": {
        "type": "object",
        "required": [
          "protocol_id",
          "schema_id",
          "archetype_slug",
          "visible",
          "side_a",
          "side_b",
          "censuses",
          "footer_clauses"
        ],
        "additionalProperties": false,
        "properties": {
          "protocol_id": { "type": "string", "pattern": "^[a-z0-9-]+$" },
          "protocol_name": { "type": ["string", "null"] },
          "schema_id": {
            "type": "string",
            "enum": [
              "db4.payments_onchain.1",
              "db4.web_discovery.1",
              "db4.crawler_policy.1",
              "db4.harnesses_rpc_ui.1",
              "db4.browser_telemetry.1",
              "db4.identity_auth_crypto.1"
            ]
          },
          "archetype_slug": {
            "type": "string",
            "enum": [
              "payments_onchain",
              "web_discovery",
              "crawler_policy",
              "harnesses_rpc_ui",
              "browser_telemetry",
              "identity_auth_crypto"
            ]
          },
          "visible": { "type": "boolean" },
          "registration_posture": {
            "anyOf": [{ "type": "null" }, { "$ref": "#/$defs/registration_posture" }]
          },
          "empty_state_note_md": { "type": ["string", "null"] },
          "empty_state_citations": { "type": "array", "items": { "$ref": "#/$defs/citation" } },
          "side_a": { "$ref": "#/$defs/table" },
          "side_b": { "$ref": "#/$defs/table" },
          "additional_tables": {
            "type": ["array", "null"],
            "items": { "$ref": "#/$defs/table" }
          },
          "censuses": { "type": "array", "items": { "$ref": "#/$defs/census" } },
          "footer_clauses": { "type": "array", "items": { "$ref": "#/$defs/footer_clause" } }
        }
      }
    }
  }
}
