{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://2bit.jp/maxforge/schema/config-v1.json",
  "title": "maxforge project configuration v1",
  "description": "Project-local object metadata used by the maxforge CLI and MCP server.",
  "type": "object",
  "additionalProperties": false,
  "required": ["schemaVersion"],
  "properties": {
    "$schema": { "type": "string" },
    "schemaVersion": { "const": 1 },
    "project": { "$ref": "#/$defs/project" },
    "catalogs": {
      "type": "array",
      "items": { "type": "string", "minLength": 1 }
    },
    "objects": {
      "type": "array",
      "items": { "$ref": "#/$defs/external" }
    },
    "abstractions": {
      "type": "array",
      "items": { "$ref": "#/$defs/abstraction" }
    }
  },
  "$defs": {
    "project": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id"],
      "properties": {
        "id": {
          "type": "string",
          "minLength": 1,
          "maxLength": 128,
          "pattern": "^[A-Za-z_][A-Za-z0-9_-]*$"
        },
        "name": {
          "type": "string",
          "minLength": 1,
          "maxLength": 256
        }
      }
    },
    "name": {
      "type": "string",
      "minLength": 1,
      "pattern": "^\\S+$"
    },
    "defaultSize": {
      "type": "array",
      "prefixItems": [
        { "type": "number", "exclusiveMinimum": 0 },
        { "type": "number", "exclusiveMinimum": 0 }
      ],
      "items": false,
      "minItems": 2,
      "maxItems": 2
    },
    "fixedPorts": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode", "inlets", "outlets"],
      "properties": {
        "mode": { "const": "fixed" },
        "inlets": { "type": "integer", "minimum": 0 },
        "outlets": {
          "type": "array",
          "items": { "type": "string" }
        }
      }
    },
    "dynamicPorts": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode", "representative"],
      "properties": {
        "mode": { "const": "dynamic" },
        "representative": {
          "type": "object",
          "additionalProperties": false,
          "required": ["inlets", "outlets"],
          "properties": {
            "inlets": { "type": "integer", "minimum": 0 },
            "outlets": {
              "type": "array",
              "items": { "type": "string" }
            }
          }
        }
      }
    },
    "inletCountRule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source"],
      "properties": {
        "source": { "enum": ["argument", "argument-count"] },
        "index": { "type": "integer", "minimum": 0 },
        "offset": { "type": "integer" },
        "minimum": { "type": "integer", "minimum": 0, "maximum": 4096 },
        "maximum": { "type": "integer", "minimum": 0, "maximum": 4096 }
      },
      "allOf": [
        {
          "if": { "properties": { "source": { "const": "argument" } } },
          "then": { "required": ["index"] }
        },
        {
          "if": { "properties": { "source": { "const": "argument-count" } } },
          "then": { "not": { "required": ["index"] } }
        }
      ]
    },
    "outletCountRule": {
      "type": "object",
      "additionalProperties": false,
      "required": ["source"],
      "properties": {
        "source": { "enum": ["argument", "argument-count"] },
        "index": { "type": "integer", "minimum": 0 },
        "offset": { "type": "integer" },
        "minimum": { "type": "integer", "minimum": 0, "maximum": 4096 },
        "maximum": { "type": "integer", "minimum": 0, "maximum": 4096 },
        "outlettype": { "type": "string" }
      },
      "allOf": [
        {
          "if": { "properties": { "source": { "const": "argument" } } },
          "then": { "required": ["index"] }
        },
        {
          "if": { "properties": { "source": { "const": "argument-count" } } },
          "then": { "not": { "required": ["index"] } }
        }
      ]
    },
    "argumentPorts": {
      "type": "object",
      "additionalProperties": false,
      "required": ["mode", "representative"],
      "properties": {
        "mode": { "const": "arguments" },
        "representative": {
          "type": "object",
          "additionalProperties": false,
          "required": ["inlets", "outlets"],
          "properties": {
            "inlets": { "type": "integer", "minimum": 0, "maximum": 4096 },
            "outlets": {
              "type": "array",
              "maxItems": 4096,
              "items": { "type": "string" }
            }
          }
        },
        "inlets": { "$ref": "#/$defs/inletCountRule" },
        "outlets": { "$ref": "#/$defs/outletCountRule" }
      },
      "anyOf": [
        { "required": ["inlets"] },
        { "required": ["outlets"] }
      ]
    },
    "ports": {
      "oneOf": [
        { "$ref": "#/$defs/fixedPorts" },
        { "$ref": "#/$defs/dynamicPorts" },
        { "$ref": "#/$defs/argumentPorts" }
      ]
    },
    "external": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "kind", "ports"],
      "properties": {
        "name": { "$ref": "#/$defs/name" },
        "kind": { "const": "external" },
        "path": {
          "oneOf": [
            { "type": "string", "minLength": 1 },
            {
              "type": "array",
              "minItems": 1,
              "items": { "type": "string", "minLength": 1 }
            }
          ]
        },
        "serialization": {
          "type": "object",
          "additionalProperties": false,
          "required": ["maxclass"],
          "properties": {
            "maxclass": { "type": "string", "minLength": 1 }
          }
        },
        "ports": { "$ref": "#/$defs/ports" },
        "defaultSize": { "$ref": "#/$defs/defaultSize" },
        "override": { "type": "boolean" }
      }
    },
    "abstraction": {
      "type": "object",
      "additionalProperties": false,
      "required": ["name", "path"],
      "properties": {
        "name": { "$ref": "#/$defs/name" },
        "path": { "type": "string", "minLength": 1, "pattern": "\\.maxpat$" },
        "ports": {
          "oneOf": [
            { "const": "derive" },
            { "$ref": "#/$defs/ports" }
          ]
        },
        "defaultSize": { "$ref": "#/$defs/defaultSize" },
        "override": { "type": "boolean" }
      }
    }
  }
}
