{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://raw.githubusercontent.com/KanadeK/rigloom/main/schema/rigloom.schema.json",
  "title": "RigLoom build",
  "description": "A declarative inventory of PC internal connector sources, component leads, hubs, and policies.",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion", "name", "sources", "components"],
  "properties": {
    "$schema": { "type": "string" },
    "schemaVersion": { "const": 1 },
    "name": { "type": "string", "minLength": 3 },
    "description": { "type": "string" },
    "policies": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "headroomPercent": { "type": "integer", "minimum": 0, "maximum": 90, "default": 20 },
        "requireCriticalSources": { "type": "boolean", "default": true },
        "maxSolverNodes": {
          "type": "integer",
          "minimum": 100,
          "maximum": 2000000,
          "default": 200000
        }
      }
    },
    "sources": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/source" }
    },
    "components": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/component" }
    },
    "connections": {
      "type": "array",
      "default": [],
      "items": {
        "type": "object",
        "additionalProperties": false,
        "required": ["requirement", "source"],
        "properties": {
          "requirement": { "$ref": "#/$defs/id" },
          "source": { "$ref": "#/$defs/id" }
        }
      }
    }
  },
  "$defs": {
    "id": {
      "type": "string",
      "pattern": "^[A-Za-z0-9][A-Za-z0-9._-]*$"
    },
    "connector": {
      "enum": [
        "fan-pwm-4pin",
        "fan-dc-3pin",
        "argb-5v-3pin",
        "rgb-12v-4pin",
        "usb2-internal-9pin",
        "usb3-internal-19pin",
        "usb-c-type-e",
        "front-audio-hd",
        "front-panel-switch",
        "sata-power",
        "molex-power",
        "temperature-2pin"
      ]
    },
    "capacity": {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "maxDevices": { "type": "integer", "minimum": 1, "default": 1 },
        "maxCurrentMa": { "type": "integer", "minimum": 0 },
        "maxLeds": { "type": "integer", "minimum": 0 }
      }
    },
    "source": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "connector"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "label": { "type": "string", "minLength": 1 },
        "connector": { "$ref": "#/$defs/connector" },
        "location": { "type": "string" },
        "traits": {
          "type": "array",
          "uniqueItems": true,
          "items": { "type": "string" }
        },
        "reservedFor": {
          "type": "array",
          "uniqueItems": true,
          "items": { "type": "string" }
        },
        "capacity": { "$ref": "#/$defs/capacity" },
        "drawFrom": { "$ref": "#/$defs/id" },
        "notes": { "type": "string" }
      }
    },
    "requirement": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "connector"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "label": { "type": "string", "minLength": 1 },
        "connector": { "$ref": "#/$defs/connector" },
        "quantity": { "type": "integer", "minimum": 1, "maximum": 64, "default": 1 },
        "currentMa": { "type": "integer", "minimum": 0, "default": 0 },
        "leds": { "type": "integer", "minimum": 0, "default": 0 },
        "requiredTraits": {
          "type": "array",
          "uniqueItems": true,
          "items": { "type": "string" }
        },
        "role": { "type": "string" },
        "preferredSources": {
          "type": "array",
          "uniqueItems": true,
          "items": { "$ref": "#/$defs/id" }
        },
        "notes": { "type": "string" }
      }
    },
    "component": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "requirements"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "label": { "type": "string", "minLength": 1 },
        "category": { "type": "string" },
        "requirements": {
          "type": "array",
          "items": { "$ref": "#/$defs/requirement" }
        },
        "outputs": {
          "type": "array",
          "items": { "$ref": "#/$defs/source" }
        }
      }
    }
  }
}
