{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://5e-spellbook.pages.dev/spellpack.schema.json",
  "title": "Spell Pack",
  "description": "Spell Pack format for Spellbook 5e app. Import additional D&D 5e spells via Settings → Spell Packs.",
  "type": "object",
  "required": ["format", "edition", "spells"],
  "additionalProperties": false,
  "properties": {
    "$schema": {
      "type": "string",
      "description": "JSON Schema reference for editor validation and autocompletion."
    },
    "format": {
      "const": 1,
      "description": "Spell Pack format version. Always 1."
    },
    "edition": {
      "type": "string",
      "enum": ["edition2014", "edition2024"],
      "description": "D&D edition. Each edition is imported separately."
    },
    "spells": {
      "type": "array",
      "description": "Array of spell objects.",
      "items": { "$ref": "#/$defs/spell" }
    }
  },
  "$defs": {
    "spell": {
      "type": "object",
      "required": [
        "id", "level", "school", "source",
        "ritual", "concentration", "components", "materials",
        "castingTime", "duration", "range", "en", "ru"
      ],
      "additionalProperties": false,
      "properties": {
        "id": {
          "type": "string",
          "description": "Unique ID: (name + source) lowercased, non-alphanumeric removed. E.g. \"fireboltphb\".",
          "pattern": "^[a-z0-9]+$"
        },
        "level": {
          "type": "integer",
          "minimum": 0,
          "maximum": 9,
          "description": "Spell level. 0 for cantrips, 1–9 for leveled spells."
        },
        "school": {
          "type": "string",
          "enum": [
            "abjuration", "conjuration", "divination", "enchantment",
            "evocation", "illusion", "necromancy", "transmutation"
          ],
          "description": "School of magic."
        },
        "source": {
          "type": "string",
          "description": "Source book abbreviation. 2014: PHB, XGE, TCE, etc. 2024: XPHB (2024 Player's Handbook)."
        },
        "ritual": {
          "type": "boolean",
          "description": "true if the spell can be cast as a ritual."
        },
        "concentration": {
          "type": "boolean",
          "description": "true if the spell requires concentration."
        },
        "components": {
          "$ref": "#/$defs/components"
        },
        "materials": {
          "oneOf": [
            { "$ref": "#/$defs/materials" },
            { "type": "null" }
          ],
          "description": "Material component details, or null if none."
        },
        "castingTime": {
          "type": "array",
          "description": "Casting time entries.",
          "items": { "$ref": "#/$defs/castingTime" },
          "minItems": 1
        },
        "duration": {
          "$ref": "#/$defs/duration"
        },
        "range": {
          "$ref": "#/$defs/range"
        },
        "en": {
          "$ref": "#/$defs/localizedContent",
          "description": "English localization."
        },
        "ru": {
          "$ref": "#/$defs/localizedContent",
          "description": "Russian localization."
        },
        "classes": {
          "type": "array",
          "description": "Classes with access to this spell. Lowercase.",
          "items": {
            "type": "string",
            "enum": [
              "artificer", "barbarian", "bard", "cleric", "druid",
              "fighter", "monk", "paladin", "ranger", "rogue",
              "sorcerer", "warlock", "wizard"
            ]
          },
          "default": []
        },
        "subclasses": {
          "type": "array",
          "description": "Subclasses with access to this spell. Lowercase, hyphenated.",
          "items": { "type": "string" },
          "default": []
        }
      }
    },
    "components": {
      "type": "object",
      "required": ["verbal", "somatic", "material"],
      "additionalProperties": false,
      "description": "Spell components: verbal (V), somatic (S), material (M).",
      "properties": {
        "verbal": { "type": "boolean", "description": "Verbal (V) component required." },
        "somatic": { "type": "boolean", "description": "Somatic (S) component required." },
        "material": { "type": "boolean", "description": "Material (M) component required. Details in the materials field." }
      }
    },
    "materials": {
      "type": "object",
      "required": ["cost", "consumed"],
      "additionalProperties": false,
      "description": "Material component details.",
      "properties": {
        "cost": {
          "oneOf": [
            { "type": "integer", "minimum": 0 },
            { "type": "null" }
          ],
          "description": "Cost in copper pieces (100 cp = 1 gp), or null if no specific cost."
        },
        "consumed": {
          "type": "boolean",
          "description": "true if the material is consumed on cast."
        }
      }
    },
    "castingTime": {
      "type": "object",
      "required": ["value", "unit"],
      "additionalProperties": false,
      "properties": {
        "value": {
          "type": "integer",
          "minimum": 1,
          "description": "Number of units."
        },
        "unit": {
          "type": "string",
          "enum": ["action", "bonus", "reaction", "minute", "hour"],
          "description": "Time unit."
        }
      }
    },
    "duration": {
      "type": "object",
      "required": ["type"],
      "additionalProperties": false,
      "description": "Spell duration.",
      "properties": {
        "type": {
          "type": "string",
          "enum": ["instant", "timed", "permanent", "special"]
        },
        "unit": {
          "type": "string",
          "enum": ["round", "minute", "hour", "day"],
          "description": "Required when type is \"timed\"."
        },
        "value": {
          "type": "integer",
          "minimum": 1,
          "description": "Number of units. Required when type is \"timed\"."
        },
        "ends": {
          "type": "array",
          "description": "How a permanent duration can end.",
          "items": {
            "type": "string",
            "enum": ["dispel", "trigger"]
          }
        }
      },
      "allOf": [
        {
          "if": { "properties": { "type": { "const": "timed" } } },
          "then": { "required": ["type", "unit", "value"] }
        }
      ]
    },
    "range": {
      "type": "object",
      "required": ["type", "distanceType"],
      "additionalProperties": false,
      "description": "Spell range.",
      "properties": {
        "type": {
          "type": "string",
          "enum": [
            "point", "cone", "line", "cube", "sphere",
            "emanation", "radius", "hemisphere", "special"
          ],
          "description": "Shape type."
        },
        "distanceType": {
          "type": "string",
          "enum": ["self", "touch", "feet", "miles", "sight", "unlimited"],
          "description": "Distance measurement type."
        },
        "distanceValue": {
          "type": "integer",
          "minimum": 0,
          "description": "Distance in the specified unit. Required for feet/miles."
        }
      }
    },
    "localizedContent": {
      "type": "object",
      "required": ["name", "text"],
      "additionalProperties": false,
      "description": "Localized spell content. Text fields support HTML.",
      "properties": {
        "name": {
          "type": "string",
          "description": "Spell name."
        },
        "text": {
          "type": "string",
          "description": "Spell description. HTML markup supported."
        },
        "higherLevels": {
          "oneOf": [
            { "type": "string" },
            { "type": "null" }
          ],
          "description": "\"At Higher Levels\" text, or null.",
          "default": null
        },
        "materialsText": {
          "oneOf": [
            { "type": "string" },
            { "type": "null" }
          ],
          "description": "Human-readable material component description, or null.",
          "default": null
        },
        "castingCondition": {
          "oneOf": [
            { "type": "string" },
            { "type": "null" }
          ],
          "description": "Reaction trigger condition, or null.",
          "default": null
        }
      }
    }
  }
}
