{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "proyecto.schema.json",
  "title": "Proyecto de breadwire (.breadwire.json), formato 1",
  "description": "Un proyecto de breadwire: analógico (esquema → protoboard) o digital (tabla de verdad → integrados 74xx). Documentación: docs/FORMATO-PROYECTO.md. Se comprueba con node tools/bw-check.cjs.",
  "type": "object",
  "required": ["kind"],
  "properties": {
    "app": { "const": "breadwire" },
    "format": { "const": 1 },
    "kind": { "enum": ["analog", "digital"] },
    "title": { "type": "string", "maxLength": 120 },
    "savedAt": { "type": "string" },
    "analog": { "$ref": "#/$defs/analog" },
    "digital": { "$ref": "#/$defs/digital" },
    "sim": { "type": "object" }
  },
  "oneOf": [
    { "properties": { "kind": { "const": "analog" } }, "required": ["analog"] },
    { "properties": { "kind": { "const": "digital" } }, "required": ["digital"] }
  ],
  "$defs": {
    "analog": {
      "type": "object",
      "description": "Bloque analógico. Lo que lee el Traductor es `netlist`; `editor` es el dibujo del esquema.",
      "properties": {
        "editor": { "type": "object" },
        "netlist": { "$ref": "#/$defs/netlist" },
        "boardConfig": {
          "type": "object",
          "properties": {
            "cols": { "type": "integer", "minimum": 5, "maximum": 200 },
            "rails": { "type": "boolean" }
          }
        },
        "batteryPositions": { "type": "object" },
        "sourcePositions": { "type": "object" }
      }
    },
    "netlist": {
      "type": "object",
      "description": "Netlist del Traductor: cada pata dice a qué red va, y cada red lista sus patas como 'REF.pata'.",
      "required": ["components", "nets"],
      "properties": {
        "components": {
          "type": "array",
          "items": {
            "type": "object",
            "required": ["ref", "type", "pins"],
            "properties": {
              "ref": { "type": "string", "pattern": "^[A-Za-z][A-Za-z0-9_]*$" },
              "type": {
                "enum": ["battery", "vsource", "isource", "vsource_ac", "isource_ac", "resistor", "switch", "led", "diode",
                         "potentiometer", "capacitor", "capacitor_el", "inductor", "fuse", "thermistor", "photodiode", "zener",
                         "buzzer", "ldr", "motor", "regulator", "pushbutton", "npn", "pnp", "transistor", "lm311", "lm358",
                         "opamp", "acs712", "transformer", "bridge", "mcu", "display"]
              },
              "pins": { "type": "object", "additionalProperties": { "type": ["string", "null"] } },
              "value": { "type": ["number", "string"] },
              "color": { "type": "string" },
              "bands": {
                "type": "object",
                "properties": { "d1": { "type": "string" }, "d2": { "type": "string" }, "mult": { "type": "string" }, "tol": { "type": "string" } }
              }
            }
          }
        },
        "nets": {
          "type": "object",
          "additionalProperties": {
            "type": "array",
            "items": { "type": "string", "pattern": "^[A-Za-z][A-Za-z0-9_]*\\.[^.]+$" }
          }
        }
      }
    },
    "digital": {
      "type": "object",
      "description": "Bloque digital. La tabla de verdad sola ya es un proyecto: la app genera el circuito mínimo y el montaje.",
      "properties": {
        "table": { "$ref": "#/$defs/table" },
        "circuit": { "type": "object" },
        "lab": { "type": "object" },
        "sheets": { "type": "object" },
        "subs": { "type": "object" },
        "pinmap": { "type": "integer" }
      }
    },
    "table": {
      "type": "object",
      "required": ["nVars", "nOuts", "cells"],
      "properties": {
        "v": { "const": 1 },
        "nVars": { "type": "integer", "minimum": 1, "maximum": 6 },
        "nOuts": { "type": "integer", "minimum": 1, "maximum": 8 },
        "impl": { "enum": ["sop", "pos", "nand", "two"] },
        "cells": {
          "type": "array",
          "description": "Una fila por combinación de entradas (2^nVars filas, la 0 es todo a 0), con una celda por salida: 0, 1 o 'x' (no importa).",
          "items": { "type": "array", "items": { "enum": [0, 1, "x"] } }
        },
        "exprs": { "type": "array", "items": { "type": "string" } }
      }
    }
  }
}
