{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://structly.local/structly.schema.json",
  "title": "Structly Diagram",
  "description": "A JSON-driven interactive architecture diagram.",
  "type": "object",
  "additionalProperties": false,
  "required": ["meta", "types", "stages", "nodes", "edges"],
  "properties": {
    "$schema": {
      "type": "string"
    },
    "meta": {
      "type": "object",
      "additionalProperties": true,
      "required": ["title"],
      "properties": {
        "kicker": { "type": "string" },
        "title": { "type": "string", "minLength": 1 },
        "subtitle": { "type": "string" },
        "version": { "type": ["integer", "string"] },
        "revision": { "type": "string" },
        "previousVersion": { "type": "string" }
      }
    },
    "types": {
      "type": "object",
      "description": "User-defined node type registry keyed by type id.",
      "minProperties": 1,
      "additionalProperties": {
        "$ref": "#/$defs/type"
      }
    },
    "stages": {
      "type": "array",
      "description": "Ordered stage lanes. Nodes listed here are rendered on the canvas.",
      "minItems": 1,
      "items": {
        "$ref": "#/$defs/stage"
      }
    },
    "nodes": {
      "description": "Node registry. Object form is preferred; array form is accepted for author convenience.",
      "oneOf": [
        {
          "type": "object",
          "minProperties": 1,
          "additionalProperties": {
            "$ref": "#/$defs/node"
          }
        },
        {
          "type": "array",
          "minItems": 1,
          "items": {
            "allOf": [
              { "$ref": "#/$defs/node" },
              {
                "type": "object",
                "required": ["id"]
              }
            ]
          }
        }
      ]
    },
    "edges": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/edge"
      }
    },
    "groups": {
      "type": "array",
      "items": {
        "$ref": "#/$defs/group"
      }
    },
    "crossCutting": {
      "type": "array",
      "description": "Optional side-panel concerns that affect multiple nodes. Group them with the per-entry \"category\" field.",
      "items": {
        "$ref": "#/$defs/panelEntry"
      }
    },
    "fileFlows": {
      "type": "array",
      "description": "Animated path definitions used by the Play control.",
      "items": {
        "$ref": "#/$defs/fileFlow"
      }
    },
    "filePaths": {
      "type": "array",
      "description": "Legacy shorthand for fileFlows. A path array is converted to adjacent step pairs at runtime.",
      "items": {
        "$ref": "#/$defs/filePath"
      }
    }
  },
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[A-Za-z0-9][A-Za-z0-9_.:-]*$"
    },
    "type": {
      "type": "object",
      "additionalProperties": false,
      "required": ["label", "color", "background"],
      "properties": {
        "label": { "type": "string", "minLength": 1 },
        "color": { "type": "string", "description": "CSS color for strokes and labels." },
        "background": { "type": "string", "description": "CSS color for node and group backgrounds." },
        "icon": { "type": "string", "description": "Reserved for future icon rendering." }
      }
    },
    "stage": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "label", "nodes"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "label": { "type": "string", "minLength": 1 },
        "nodes": {
          "type": "array",
          "items": { "$ref": "#/$defs/id" }
        }
      }
    },
    "node": {
      "type": "object",
      "additionalProperties": true,
      "required": ["type"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "type": { "$ref": "#/$defs/id" },
        "title": { "type": "string" },
        "label": { "type": "string" },
        "summary": { "type": "string" },
        "sublabel": { "type": "string" },
        "description": { "type": "string" },
        "icon": { "type": "string", "description": "Emoji or short glyph shown on the node card." },
        "external": { "type": "boolean", "description": "Marks a third-party / out-of-boundary system; rendered with a dashed border + EXT flag." },
        "link": {
          "description": "Drill-down to another diagram (string path, or an object with id/diagram/url/label).",
          "oneOf": [
            { "type": "string" },
            {
              "type": "object",
              "additionalProperties": false,
              "properties": {
                "id": { "type": "string" },
                "diagram": { "type": "string" },
                "url": { "type": "string" },
                "label": { "type": "string" }
              }
            }
          ]
        },
        "details": {
          "type": "object",
          "additionalProperties": {
            "type": ["string", "number", "boolean"]
          }
        },
        "topics": {
          "type": "array",
          "items": { "type": "string" }
        },
        "queries": {
          "type": "array",
          "items": { "type": "string" }
        },
        "code": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "properties": {
              "label": { "type": "string" },
              "language": { "type": "string" },
              "snippet": { "type": "string" },
              "content": { "type": "string" }
            }
          }
        }
      },
      "anyOf": [
        { "required": ["title"] },
        { "required": ["label"] }
      ]
    },
    "edge": {
      "type": "object",
      "additionalProperties": false,
      "required": ["from", "to"],
      "properties": {
        "from": { "$ref": "#/$defs/id" },
        "to": { "$ref": "#/$defs/id" },
        "label": { "type": "string" },
        "kind": {
          "type": "string",
          "description": "Optional style hint. Known values include normal, consult, return, control, raw-write, and group-flow."
        },
        "protocol": {
          "type": "string",
          "description": "Transport/protocol hint shown on the edge label, e.g. HTTP, gRPC, Kafka, SQL."
        },
        "async": {
          "type": "boolean",
          "description": "Render as an asynchronous (dashed) edge."
        },
        "animated": { "type": "boolean" }
      }
    },
    "group": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "label", "type"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "label": { "type": "string", "minLength": 1 },
        "type": { "$ref": "#/$defs/id" },
        "nodes": {
          "type": "array",
          "items": { "$ref": "#/$defs/id" }
        },
        "subgroups": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["label", "nodes"],
            "properties": {
              "id": { "$ref": "#/$defs/id" },
              "label": { "type": "string" },
              "nodes": {
                "type": "array",
                "items": { "$ref": "#/$defs/id" }
              }
            }
          }
        },
        "flowsTo": {
          "type": "array",
          "items": { "$ref": "#/$defs/id" }
        }
      },
      "anyOf": [
        { "required": ["nodes"] },
        { "required": ["subgroups"] }
      ]
    },
    "panelEntry": {
      "type": "object",
      "additionalProperties": true,
      "required": ["id", "label", "type"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "badge": { "type": "string" },
        "shortBadge": { "type": "string", "description": "Short text shown inside the cross-cutting badge." },
        "badgeColor": { "type": "string", "description": "CSS color for the badge text and border." },
        "badgeBackground": { "type": "string", "description": "CSS color for the badge background." },
        "shortBadgeColor": { "type": "string", "description": "Alias for badgeColor." },
        "shortBadgeBackground": { "type": "string", "description": "Alias for badgeBackground." },
        "label": { "type": "string" },
        "summary": { "type": "string" },
        "category": { "type": "string" },
        "type": { "$ref": "#/$defs/id" },
        "note": { "type": "string" },
        "description": { "type": "string" },
        "details": {
          "type": "object",
          "additionalProperties": {
            "type": ["string", "number", "boolean"]
          }
        },
        "codeSnippet": {
          "type": "object",
          "additionalProperties": true,
          "properties": {
            "lang": { "type": "string" },
            "language": { "type": "string" },
            "code": { "type": "string" },
            "snippet": { "type": "string" }
          }
        },
        "affects": {
          "type": "array",
          "items": { "$ref": "#/$defs/id" }
        },
        "nodes": {
          "type": "array",
          "description": "Alias for affects.",
          "items": { "$ref": "#/$defs/id" }
        }
      }
    },
    "fileFlow": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "label", "steps"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "label": { "type": "string" },
        "color": { "type": "string" },
        "steps": {
          "type": "array",
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["from", "to"],
            "properties": {
              "from": { "$ref": "#/$defs/id" },
              "to": { "$ref": "#/$defs/id" },
              "label": { "type": "string" }
            }
          }
        }
      }
    },
    "filePath": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "label", "path"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "label": { "type": "string" },
        "color": { "type": "string" },
        "path": {
          "type": "array",
          "minItems": 2,
          "items": { "$ref": "#/$defs/id" }
        }
      }
    }
  }
}
