{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://replacor.com/schemas/electroia-diagram-spec-1.0.json",
  "title": "ElectroIA AI-friendly diagram specification",
  "description": "Especificación de alto nivel que ElectroIA compila a un documento neutral validado y a SVG.",
  "type": "object",
  "additionalProperties": false,
  "required": ["title", "components", "nets"],
  "properties": {
    "title": { "type": "string", "minLength": 1, "maxLength": 160 },
    "document_kind": { "enum": ["circuit_diagram", "single_line_diagram", "multi_line_diagram"], "default": "circuit_diagram" },
    "document_id": { "type": "string", "maxLength": 80 },
    "revision": { "type": "string", "maxLength": 20 },
    "notes": { "type": "array", "maxItems": 6, "items": { "type": "string", "maxLength": 160 } },
    "strict_resolution": { "type": "boolean", "default": false },
    "components": {
      "type": "array",
      "minItems": 1,
      "maxItems": 200,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["id"],
        "anyOf": [{ "required": ["symbol_id"] }, { "required": ["symbol_query"] }],
        "properties": {
          "id": { "type": "string", "pattern": "^[A-Za-z][A-Za-z0-9_-]{0,31}$" },
          "ref": { "type": "string", "pattern": "^[A-Za-z][A-Za-z0-9_.-]{0,31}$" },
          "symbol_id": { "type": "string", "minLength": 3, "maxLength": 64 },
          "symbol_query": { "type": "string", "minLength": 2, "maxLength": 120 },
          "value": { "type": "string", "maxLength": 120 },
          "position": { "$ref": "#/$defs/position" },
          "rotation": { "enum": [0, 90, 180, 270] },
          "mirror": { "type": "boolean" },
          "role": { "type": "string", "maxLength": 60 },
          "manufacturer": { "type": "string", "maxLength": 80 },
          "model": { "type": "string", "maxLength": 120 },
          "part_number": { "type": "string", "maxLength": 120 },
          "exact_model": { "type": "string", "maxLength": 120 },
          "location": { "type": "string", "maxLength": 80 },
          "mounting": { "type": "string", "maxLength": 80 }
        }
      }
    },
    "nets": {
      "type": "array",
      "minItems": 1,
      "maxItems": 400,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["id", "connections"],
        "properties": {
          "id": { "type": "string", "pattern": "^[A-Za-z0-9_+.-]{1,64}$" },
          "label": { "type": "string", "maxLength": 80 },
          "show_label": { "type": "boolean" },
          "role": { "enum": ["signal", "power", "ground", "protective_earth", "bus"] },
          "conductors": { "type": "integer", "minimum": 1, "maximum": 99 },
          "wire_number": { "type": "string", "minLength": 1, "maxLength": 24 },
          "conductor_size_mm2": { "type": "number", "exclusiveMinimum": 0, "maximum": 1000 },
          "color": { "type": "string", "maxLength": 40 },
          "cable_id": { "type": "string", "maxLength": 40 },
          "cable_type": { "type": "string", "maxLength": 80 },
          "voltage": { "type": "string", "maxLength": 40 },
          "signal_type": { "type": "string", "maxLength": 80 },
          "io_address": { "type": "string", "maxLength": 40 },
          "connections": {
            "type": "array",
            "minItems": 1,
            "maxItems": 100,
            "items": {
              "oneOf": [
                { "type": "string", "minLength": 3, "maxLength": 80, "description": "COMPONENT_ID.PORT" },
                {
                  "type": "object",
                  "additionalProperties": false,
                  "required": ["component", "port"],
                  "properties": {
                    "component": { "type": "string", "minLength": 1, "maxLength": 32 },
                    "port": { "type": "string", "minLength": 1, "maxLength": 24 }
                  }
                }
              ]
            }
          }
        }
      }
    },
    "relationships": {
      "type": "array",
      "maxItems": 400,
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["from", "to", "kind"],
        "properties": {
          "from": { "type": "string", "minLength": 1, "maxLength": 32 },
          "to": { "type": "string", "minLength": 1, "maxLength": 32 },
          "kind": { "enum": ["mechanical", "functional"] },
          "via": { "type": "array", "items": { "$ref": "#/$defs/position" } }
        }
      }
    },
    "layout": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "direction": { "enum": ["left_to_right", "top_to_bottom"] },
        "single_canvas": { "const": true }
      }
    }
  },
  "$defs": {
    "position": {
      "type": "object",
      "additionalProperties": false,
      "required": ["x", "y"],
      "properties": {
        "x": { "type": "integer", "minimum": 0, "maximum": 500 },
        "y": { "type": "integer", "minimum": 0, "maximum": 500 }
      }
    }
  }
}
