{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "/ai/level-format.schema.json",
  "title": "MINIMA AI Level",
  "type": "object",
  "required": ["format", "name", "stages"],
  "additionalProperties": true,
  "properties": {
    "format": { "const": "minima.ai-level/v1" },
    "name": { "type": "string", "minLength": 1 },
    "description": { "type": "string" },
    "intent": { "$ref": "#/$defs/intent" },
    "challengeGraph": { "$ref": "#/$defs/challengeGraph" },
    "stages": {
      "type": "array",
      "minItems": 1,
      "items": { "$ref": "#/$defs/stage" }
    }
  },
  "$defs": {
    "tile": {
      "type": "string",
      "description": "A two-character runtime tile, __ for void, or a supported semantic alias."
    },
    "point": {
      "type": "array",
      "minItems": 2,
      "maxItems": 2,
      "prefixItems": [{ "type": "integer", "minimum": 0 }, { "type": "integer", "minimum": 0 }],
      "items": false
    },
    "size": {
      "type": "object",
      "required": ["width", "height"],
      "properties": {
        "width": { "type": "integer", "minimum": 1, "maximum": 4096 },
        "height": { "type": "integer", "minimum": 1, "maximum": 4096 }
      },
      "additionalProperties": false
    },
    "intent": {
      "type": "object",
      "required": ["difficulty", "puzzleThesis", "theme", "regions", "challengePlan", "criticalPath", "decoys"],
      "properties": {
        "difficulty": { "type": "string", "minLength": 1 },
        "puzzleThesis": { "type": "string", "minLength": 1 },
        "theme": {
          "type": "object",
          "required": ["name", "primaryMechanic", "primaryTiles", "supportingMechanics", "tilePalette", "spatialMotif"],
          "properties": {
            "name": { "type": "string", "minLength": 1 },
            "primaryMechanic": { "type": "string", "minLength": 1 },
            "primaryTiles": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/tile" } },
            "supportingMechanics": { "type": "array", "maxItems": 2, "items": { "type": "string" } },
            "tilePalette": { "type": "array", "minItems": 5, "items": { "$ref": "#/$defs/tile" } },
            "spatialMotif": { "type": "string", "minLength": 1 }
          },
          "additionalProperties": false
        },
        "regions": {
          "type": "array",
          "minItems": 2,
          "items": {
            "type": "object",
            "required": ["id", "role"],
            "properties": {
              "id": { "type": "string", "minLength": 1 },
              "role": { "type": "string", "minLength": 1 },
              "requires": { "type": "array", "items": { "type": "string" } }
            },
            "additionalProperties": false
          }
        },
        "challengePlan": {
          "type": "array",
          "minItems": 1,
          "maxItems": 5,
          "items": {
            "type": "object",
            "required": ["id", "dependsOn", "enabler", "gate", "unlocks", "protectedTiles", "bypassChecks"],
            "properties": {
              "id": { "type": "string", "minLength": 1 },
              "dependsOn": { "type": "array", "items": { "type": "string" } },
              "enabler": { "type": "string", "minLength": 1 },
              "gate": { "type": "string", "minLength": 1 },
              "unlocks": { "type": "string", "minLength": 1 },
              "protectedTiles": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/point" } },
              "bypassChecks": { "type": "array", "items": { "enum": ["walk", "fall", "jump", "teleporter", "water", "stage-transition"] } }
            },
            "additionalProperties": false
          }
        },
        "criticalPath": { "type": "array", "minItems": 3, "items": { "type": "string" } },
        "decoys": {
          "type": "array",
          "maxItems": 3,
          "items": {
            "type": "object",
            "required": ["id", "availableAfter", "entry", "endpoint", "promise", "resolution", "reward", "softlock"],
            "properties": {
              "id": { "type": "string", "minLength": 1 },
              "availableAfter": { "type": "string", "minLength": 1 },
              "entry": { "$ref": "#/$defs/point" },
              "endpoint": { "$ref": "#/$defs/point" },
              "promise": { "type": "string", "minLength": 1 },
              "resolution": { "type": "string", "minLength": 1 },
              "reward": { "type": "string", "minLength": 1 },
              "softlock": { "const": false }
            },
            "additionalProperties": false
          }
        },
        "teaching": { "type": "array", "items": { "type": "string" } },
        "targetMoves": { "type": "string" }
      },
      "additionalProperties": false
    },
    "stateChange": {
      "type": "object",
      "required": ["action"],
      "properties": {
        "action": { "enum": ["collect_key", "activate_plate", "activate_switch", "deactivate_switch", "move_cube", "transform_material"] },
        "key": { "type": "string" },
        "position": { "$ref": "#/$defs/point" },
        "cubeFrom": { "$ref": "#/$defs/point" },
        "from": { "$ref": "#/$defs/point" },
        "to": { "$ref": "#/$defs/point" },
        "material": { "enum": ["metal", "wood", "redstone"] }
      },
      "additionalProperties": false
    },
    "challengeGraph": {
      "type": "object",
      "required": ["challenges", "solveOrder", "traps"],
      "properties": {
        "challenges": {
          "type": "array",
          "minItems": 1,
          "items": {
            "type": "object",
            "required": ["id", "primitive", "activationTile", "protectedTiles", "stateChange"],
            "properties": {
              "id": { "type": "string", "minLength": 1 },
              "primitive": { "type": "string", "minLength": 1 },
              "activationTile": { "$ref": "#/$defs/point" },
              "protectedTiles": { "type": "array", "minItems": 1, "items": { "$ref": "#/$defs/point" } },
              "stateChange": { "$ref": "#/$defs/stateChange" }
            },
            "additionalProperties": false
          }
        },
        "solveOrder": { "type": "array", "minItems": 1, "items": { "type": "string" } },
        "traps": { "type": "array" }
      },
      "additionalProperties": false
    },
    "operation": {
      "type": "object",
      "required": ["op", "tile"],
      "properties": {
        "op": { "enum": ["fill", "rect", "border", "line", "path", "points", "set"] },
        "tile": { "$ref": "#/$defs/tile" },
        "x": { "type": "integer", "minimum": 0 },
        "z": { "type": "integer", "minimum": 0 },
        "width": { "type": "integer", "minimum": 1 },
        "height": { "type": "integer", "minimum": 1 },
        "from": { "$ref": "#/$defs/point" },
        "to": { "$ref": "#/$defs/point" },
        "points": { "type": "array", "items": { "$ref": "#/$defs/point" } }
      },
      "additionalProperties": false
    },
    "stage": {
      "type": "object",
      "required": ["name"],
      "properties": {
        "name": { "type": "string" },
        "description": { "type": "string" },
        "size": { "$ref": "#/$defs/size" },
        "width": { "type": "integer", "minimum": 1, "maximum": 4096 },
        "height": { "type": "integer", "minimum": 1, "maximum": 4096 },
        "default": { "$ref": "#/$defs/tile" },
        "grid": {
          "type": "array",
          "items": {
            "oneOf": [
              { "type": "string" },
              { "type": "array", "items": { "$ref": "#/$defs/tile" } }
            ]
          }
        },
        "operations": { "type": "array", "items": { "$ref": "#/$defs/operation" } },
        "quizzes": { "type": "object" },
        "questionPool": { "type": "array" }
      },
      "oneOf": [
        { "required": ["grid"] },
        { "required": ["size", "operations"] }
      ],
      "additionalProperties": false
    }
  }
}
