{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://schemeta.dev/schema/schemeta.schema.json",
  "title": "Schemeta JSON Model (SJM)",
  "type": "object",
  "additionalProperties": false,
  "required": ["meta", "symbols", "instances", "nets", "constraints", "annotations"],
  "properties": {
    "meta": {
      "type": "object",
      "description": "Project metadata.",
      "additionalProperties": true
    },
    "symbols": {
      "type": "object",
      "description": "Map of symbol_id -> symbol definition.",
      "patternProperties": {
        "^[A-Za-z_][A-Za-z0-9_]*$": {
          "$ref": "#/$defs/symbol"
        }
      },
      "additionalProperties": false
    },
    "instances": {
      "type": "array",
      "items": { "$ref": "#/$defs/instance" }
    },
    "nets": {
      "type": "array",
      "items": { "$ref": "#/$defs/net" }
    },
    "constraints": { "$ref": "#/$defs/constraints" },
    "annotations": {
      "type": "array",
      "items": { "$ref": "#/$defs/annotation" }
    }
  },
  "$defs": {
    "pinType": {
      "type": "string",
      "enum": ["power_in", "power_out", "input", "output", "bidirectional", "passive", "analog", "ground"]
    },
    "pinSide": {
      "type": "string",
      "enum": ["left", "right", "top", "bottom"]
    },
    "netClass": {
      "type": "string",
      "enum": ["power", "ground", "signal", "analog", "differential", "clock", "bus"]
    },
    "pin": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "number", "side", "offset", "type"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "number": { "type": "string", "minLength": 1 },
        "side": { "$ref": "#/$defs/pinSide" },
        "offset": { "type": "number", "minimum": 0 },
        "type": { "$ref": "#/$defs/pinType" }
      }
    },
    "symbol": {
      "type": "object",
      "additionalProperties": true,
      "description": "Symbol definition. Minimal shorthand is supported for template/generic symbols; compiler hydrates missing fields.",
      "required": [],
      "properties": {
        "symbol_id": {
          "type": "string",
          "minLength": 1,
          "description": "Optional in shorthand; defaults to map key."
        },
        "category": {
          "type": "string",
          "minLength": 1,
          "description": "Optional in shorthand; inferred from template or defaults to generic."
        },
        "auto_generated": { "type": "boolean" },
        "template_name": {
          "type": "string",
          "enum": ["resistor", "capacitor", "inductor", "diode", "led", "connector"]
        },
        "body": {
          "type": "object",
          "additionalProperties": false,
          "required": ["width", "height"],
          "properties": {
            "width": { "type": "number", "minimum": 20 },
            "height": { "type": "number", "minimum": 20 }
          }
        },
        "pins": {
          "type": "array",
          "minItems": 1,
          "items": { "$ref": "#/$defs/pin" }
        },
        "graphics": {
          "type": "object",
          "additionalProperties": true,
          "properties": {
            "primitives": {
              "type": "array",
              "items": {
                "type": "object",
                "additionalProperties": true,
                "required": ["type"],
                "properties": {
                  "type": { "type": "string" }
                }
              }
            }
          }
        }
      },
      "examples": [
        { "template_name": "resistor" },
        { "category": "generic" },
        {
          "symbol_id": "opamp_generic",
          "category": "analog",
          "body": { "width": 160, "height": 120 },
          "pins": [
            { "name": "IN+", "number": "1", "side": "left", "offset": 24, "type": "analog" },
            { "name": "IN-", "number": "2", "side": "left", "offset": 48, "type": "analog" },
            { "name": "OUT", "number": "3", "side": "right", "offset": 36, "type": "output" }
          ]
        }
      ]
    },
    "placement": {
      "type": "object",
      "additionalProperties": false,
      "required": ["x", "y", "rotation", "locked"],
      "properties": {
        "x": { "type": ["number", "null"] },
        "y": { "type": ["number", "null"] },
        "rotation": { "type": "number" },
        "locked": { "type": "boolean" }
      }
    },
    "instance": {
      "type": "object",
      "additionalProperties": false,
      "required": ["ref", "properties", "placement"],
      "anyOf": [{ "required": ["symbol"] }, { "required": ["part"] }],
      "properties": {
        "ref": { "type": "string", "pattern": "^[A-Za-z][A-Za-z0-9_]*$" },
        "symbol": {
          "type": "string",
          "minLength": 1,
          "description": "Custom symbol id reference."
        },
        "part": {
          "type": "string",
          "description": "Built-in shorthand for common parts (no explicit symbol definition required).",
          "enum": ["resistor", "capacitor", "inductor", "diode", "led", "connector", "generic"]
        },
        "properties": {
          "type": "object",
          "description": "Instance-level editable properties. Includes UI/editor hints such as per-pin label visibility.",
          "properties": {
            "pin_ui": {
              "type": "object",
              "description": "Per-pin UI overrides keyed by pin name.",
              "additionalProperties": {
                "type": "object",
                "additionalProperties": false,
                "properties": {
                  "show_net_label": { "type": "boolean" }
                }
              }
            }
          },
          "additionalProperties": {
            "type": ["string", "number", "boolean", "null", "object", "array"]
          }
        },
        "placement": { "$ref": "#/$defs/placement" }
      },
      "examples": [
        {
          "ref": "R1",
          "part": "resistor",
          "properties": { "value": "10k", "pin_ui": { "1": { "show_net_label": true } } },
          "placement": { "x": null, "y": null, "rotation": 0, "locked": false }
        },
        {
          "ref": "U1",
          "symbol": "esp32_s3_supermini",
          "properties": { "value": "ESP32-S3" },
          "placement": { "x": null, "y": null, "rotation": 0, "locked": false }
        }
      ]
    },
    "netNode": {
      "type": "object",
      "additionalProperties": false,
      "required": ["ref", "pin"],
      "properties": {
        "ref": { "type": "string", "pattern": "^[A-Za-z][A-Za-z0-9_]*$" },
        "pin": { "type": "string", "minLength": 1 }
      }
    },
    "net": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "class", "nodes"],
      "properties": {
        "name": { "type": "string", "minLength": 1 },
        "class": { "$ref": "#/$defs/netClass" },
        "nodes": {
          "type": "array",
          "minItems": 2,
          "items": { "$ref": "#/$defs/netNode" }
        }
      }
    },
    "constraintGroup": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "members", "layout"],
      "properties": {
        "name": { "type": "string" },
        "members": {
          "type": "array",
          "items": { "type": "string" }
        },
        "layout": { "type": "string", "enum": ["cluster"] }
      }
    },
    "alignmentConstraint": {
      "type": "object",
      "additionalProperties": false,
      "required": ["left_of", "right_of"],
      "properties": {
        "left_of": { "type": "string" },
        "right_of": { "type": "string" }
      }
    },
    "nearConstraint": {
      "type": "object",
      "additionalProperties": false,
      "required": ["component", "target_pin"],
      "properties": {
        "component": { "type": "string" },
        "target_pin": {
          "type": "object",
          "additionalProperties": false,
          "required": ["ref", "pin"],
          "properties": {
            "ref": { "type": "string" },
            "pin": { "type": "string" }
          }
        }
      }
    },
    "constraints": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "groups": {
          "type": "array",
          "items": { "$ref": "#/$defs/constraintGroup" }
        },
        "alignment": {
          "type": "array",
          "items": { "$ref": "#/$defs/alignmentConstraint" }
        },
        "near": {
          "type": "array",
          "items": { "$ref": "#/$defs/nearConstraint" }
        }
      },
      "default": {}
    },
    "annotation": {
      "type": "object",
      "additionalProperties": false,
      "required": ["text"],
      "properties": {
        "text": { "type": "string", "minLength": 1 },
        "x": { "type": "number" },
        "y": { "type": "number" }
      }
    }
  }
}
