{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://abraxasdigital.com/schemas/abraxas-scene-import-v1.schema.json",
  "title": "ABRAXAS Scene Import v1",
  "type": "object",
  "additionalProperties": false,
  "required": ["format", "version", "scene", "nodes"],
  "properties": {
    "format": { "const": "abraxas.scene-import" },
    "version": { "const": 1 },
    "scene": {
      "type": "object",
      "additionalProperties": false,
      "required": ["title", "start"],
      "properties": {
        "title": { "type": "string", "minLength": 1, "maxLength": 160 },
        "start": { "$ref": "#/$defs/id" }
      }
    },
    "nodes": {
      "type": "array",
      "minItems": 1,
      "maxItems": 3000,
      "items": { "oneOf": [
        { "$ref": "#/$defs/narrative" },
        { "$ref": "#/$defs/dialogue" },
        { "$ref": "#/$defs/choice" },
        { "$ref": "#/$defs/end" }
      ] }
    }
  },
  "$defs": {
    "id": { "type": "string", "pattern": "^[a-zA-Z][a-zA-Z0-9_-]{0,63}$" },
    "text": { "type": "string", "minLength": 1, "maxLength": 4000 },
    "span": {
      "type": "object",
      "additionalProperties": false,
      "required": ["start", "end", "style"],
      "properties": {
        "start": { "type": "integer", "minimum": 0 },
        "end": { "type": "integer", "minimum": 1 },
        "style": { "enum": ["bold", "italic"] }
      }
    },
    "formatting": { "type": "array", "maxItems": 200, "items": { "$ref": "#/$defs/span" } },
    "destination": { "oneOf": [{ "$ref": "#/$defs/id" }, { "type": "null" }] },
    "layout": {
      "type": "object",
      "additionalProperties": false,
      "required": ["attachTo", "side"],
      "properties": { "attachTo": { "$ref": "#/$defs/id" }, "side": { "enum": ["left", "right"] } }
    },
    "narrative": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "text", "next"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "type": { "enum": ["description", "action"] },
        "text": { "$ref": "#/$defs/text" },
        "formatting": { "$ref": "#/$defs/formatting" },
        "layout": { "$ref": "#/$defs/layout" },
        "next": { "$ref": "#/$defs/destination" }
      },
      "allOf": [
        { "if": { "properties": { "type": { "const": "description" } } }, "then": { "properties": { "layout": { "properties": { "side": { "const": "left" } } } } } },
        { "if": { "properties": { "type": { "const": "action" } } }, "then": { "properties": { "layout": { "properties": { "side": { "const": "right" } } } } } }
      ]
    },
    "dialogue": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "speaker", "text", "next"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "type": { "const": "dialogue" },
        "speaker": { "type": "string", "minLength": 1, "maxLength": 100 },
        "text": { "$ref": "#/$defs/text" },
        "formatting": { "$ref": "#/$defs/formatting" },
        "next": { "$ref": "#/$defs/destination" }
      }
    },
    "choice": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type", "options"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "type": { "const": "choice" },
        "speaker": { "type": "string", "maxLength": 100 },
        "text": { "type": "string", "maxLength": 4000 },
        "formatting": { "$ref": "#/$defs/formatting" },
        "options": {
          "type": "array",
          "minItems": 1,
          "maxItems": 20,
          "items": {
            "type": "object",
            "additionalProperties": false,
            "required": ["id", "text", "next"],
            "properties": {
              "id": { "$ref": "#/$defs/id" },
              "text": { "type": "string", "minLength": 1, "maxLength": 1000 },
              "formatting": { "$ref": "#/$defs/formatting" },
              "next": { "$ref": "#/$defs/destination" }
            }
          }
        }
      }
    },
    "end": {
      "type": "object",
      "additionalProperties": false,
      "required": ["id", "type"],
      "properties": {
        "id": { "$ref": "#/$defs/id" },
        "type": { "const": "end" }
      }
    }
  }
}
