{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://github.com/welkaim/ea-codex/schemas/v1/reference-architecture.json",
  "title": "ReferenceArchitecture",
  "description": "Typed reference architecture with layers, required components, relationships, invariants, and rendering metadata. Introduced in Chapter 6. The diagram is a rendered view of this declaration, not a separately authored asset. Uses the standard metadata + spec envelope.",
  "type": "object",
  "required": ["apiVersion", "kind", "metadata", "spec"],
  "properties": {
    "apiVersion": { "$ref": "_common.json#/$defs/apiVersion" },
    "kind": { "type": "string", "const": "ReferenceArchitecture" },
    "metadata": { "$ref": "_common.json#/$defs/metadata" },
    "spec": {
      "type": "object",
      "required": ["appliesTo", "layers"],
      "properties": {
        "appliesTo": {
          "type": "object",
          "description": "Filter that selects which fact-sheet instances this reference architecture governs.",
          "properties": {
            "factSheetTypes": {
              "type": "array",
              "items": { "type": "string" }
            },
            "filter": { "type": "object", "additionalProperties": true }
          },
          "additionalProperties": true
        },
        "layers": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "required": ["id", "position", "role"],
            "properties": {
              "id": { "type": "string" },
              "position": { "type": "integer", "minimum": 1 },
              "role": { "type": "string" },
              "requiredComponents": {
                "type": "array",
                "items": { "type": "string" }
              },
              "optionalComponents": {
                "type": "array",
                "items": { "type": "string" }
              }
            },
            "additionalProperties": true
          }
        },
        "relationships": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["from", "to", "type"],
            "properties": {
              "from": { "type": "string" },
              "to": { "type": "string" },
              "via": { "type": "string" },
              "type": {
                "type": "string",
                "description": "Free text relationship type. Common values: readOnly, gatedWrite, preActionCheck, asyncEvent."
              },
              "policyBinding": { "type": "string" },
              "blockingOnDeny": { "type": "boolean" }
            },
            "additionalProperties": true
          }
        },
        "invariants": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["id", "rule", "severity"],
            "properties": {
              "id": { "type": "string" },
              "rule": { "type": "string" },
              "severity": {
                "type": "string",
                "enum": ["info", "low", "medium", "high", "critical"]
              }
            },
            "additionalProperties": true
          }
        },
        "renderedBy": {
          "type": "object",
          "properties": {
            "skill": { "type": "string" },
            "inputSource": { "type": "string" },
            "output": { "type": "string" }
          },
          "additionalProperties": true
        },
        "derivedFromPrinciples": {
          "type": "array",
          "items": { "type": "string" }
        }
      },
      "additionalProperties": true
    }
  },
  "additionalProperties": false
}
