{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://velacent.com/schemas/config-bundle/1/config-bundle.schema.json",
  "title": "Velacent Bridge configuration bundle, format version 1",
  "description": "One JSON document that holds a tenant's whole configuration, or one route and everything it depends on, for backup, download and restore into the same or a new account. Never contains secrets, keys, tokens, passwords, PHI, message payloads, audit records or users. The digest is the SHA-256 of the canonical JSON of 'content' (object keys sorted, no insignificant whitespace, numbers as written, no HTML escaping). See docs/CONFIG_BUNDLE.md.",
  "type": "object",
  "additionalProperties": false,
  "required": ["format", "format_version", "product_version", "created_at", "source_tenant", "scope", "digest", "content"],
  "properties": {
    "format": {"const": "velacent-config-bundle"},
    "format_version": {"type": "integer", "minimum": 1, "maximum": 1},
    "product_version": {"type": "string", "maxLength": 100},
    "created_at": {"type": "string", "format": "date-time"},
    "source_tenant": {"type": "string", "maxLength": 200, "description": "Opaque identifier of the exporting account; not needed to restore."},
    "scope": {"enum": ["tenant", "route"]},
    "route": {"type": "string", "maxLength": 200, "description": "The route a route-scope bundle was made for; present only for scope 'route'."},
    "digest": {
      "type": "object",
      "additionalProperties": false,
      "required": ["algorithm", "value"],
      "properties": {
        "algorithm": {"const": "sha256"},
        "value": {"type": "string", "pattern": "^[0-9a-f]{64}$"}
      }
    },
    "content": {
      "type": "object",
      "additionalProperties": false,
      "required": ["entities", "closure", "external_dependencies", "must_supply", "requirements", "excluded", "redacted"],
      "properties": {
        "entities": {
          "type": "array",
          "maxItems": 5000,
          "description": "Configuration documents ordered by kind (runtime_settings, authority_settings, mllp_sender, ip_sender_group, code_set, code_map, script, feature_flag, notification_channel, sftp_connection, cloudftp_inbox, database_reader, route) then id.",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["kind", "id", "document"],
            "properties": {
              "kind": {"enum": ["runtime_settings", "authority_settings", "mllp_sender", "ip_sender_group", "code_set", "code_map", "script", "feature_flag", "notification_channel", "sftp_connection", "cloudftp_inbox", "database_reader", "object_storage_source", "route"]},
              "id": {"type": "string", "minLength": 1, "maxLength": 200},
              "document": {"type": "object", "description": "The entity as the versioned configuration stores it (the same documents 'velacent -config-export' writes), with keys sorted."}
            }
          }
        },
        "closure": {
          "type": "array",
          "description": "Route scope: the route and each entity it depends on. Empty for scope 'tenant'.",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["kind", "id", "role"],
            "properties": {
              "kind": {"type": "string"},
              "id": {"type": "string"},
              "role": {"enum": ["route", "dependency"]},
              "required_by": {"type": "array", "items": {"type": "string"}}
            }
          }
        },
        "external_dependencies": {
          "type": "array",
          "description": "References to things a bundle never carries (provider directories) or only names (interface profiles).",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["kind", "id", "required_by"],
            "properties": {
              "kind": {"enum": ["provider_directory", "profile"]},
              "id": {"type": "string"},
              "version": {"type": "string"},
              "sha256": {"type": "string"},
              "required_by": {"type": "array", "items": {"type": "string"}},
              "note": {"type": "string"}
            }
          }
        },
        "must_supply": {
          "type": "array",
          "description": "Values the target account must provide before restored entities can be enabled: secret and other files named by reference, sender client certificates, attestations. Never the values themselves.",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["kind", "id", "path", "type", "note"],
            "properties": {
              "kind": {"type": "string"},
              "id": {"type": "string"},
              "path": {"type": "string", "description": "JSON Pointer into the entity's document."},
              "type": {"enum": ["secret_file", "file", "certificate", "attestation"]},
              "reference": {"type": "string"},
              "note": {"type": "string"}
            }
          }
        },
        "requirements": {
          "type": "array",
          "description": "Edition features the configuration uses. A restore into an account without them lists the entity as blocked.",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["feature", "needs", "kind", "id"],
            "properties": {
              "feature": {"enum": ["external_empi", "snomed_ct"]},
              "needs": {"type": "string"},
              "kind": {"type": "string"},
              "id": {"type": "string"}
            }
          }
        },
        "excluded": {
          "type": "array",
          "description": "What this bundle never contains, and why.",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["item", "reason"],
            "properties": {
              "item": {"type": "string"},
              "count": {"type": "integer", "minimum": 1},
              "reason": {"type": "string"}
            }
          }
        },
        "redacted": {
          "type": "array",
          "description": "'kind/id /json/pointer' of inline credential values replaced by the redaction marker. A restore refuses those entities.",
          "items": {"type": "string"}
        }
      }
    }
  }
}
