{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "$id": "lexicon-schema-v2.1",
  "title": "Bible Lexicon Schema",
  "description": "Unified schema for Bible dictionaries, lexicons, and commentaries. Version 2.1 adds media support for embedded images and audio.",

  "definitions": {
    "mediaReference": {
      "type": "object",
      "description": "Reference to a media file (image or audio)",
      "properties": {
        "id": { "type": "string", "description": "Unique media identifier (UUID)" },
        "type": {
          "type": "string",
          "enum": ["image", "audio"],
          "description": "Media type"
        },
        "filename": { "type": "string", "description": "Original filename" },
        "mimeType": { "type": "string", "description": "MIME type (e.g., image/jpeg, audio/m4a)" },
        "size": { "type": "integer", "description": "File size in bytes" },
        "width": { "type": "integer", "description": "Image width in pixels (images only)" },
        "height": { "type": "integer", "description": "Image height in pixels (images only)" },
        "duration": { "type": "number", "description": "Duration in seconds (audio only)" },
        "waveform": {
          "type": "array",
          "items": { "type": "number" },
          "description": "Normalized audio waveform samples 0-1 for visualization (audio only)"
        },
        "transcription": { "type": "string", "description": "Optional speech-to-text transcription (audio only)" },
        "alt": { "type": "string", "description": "Alt text for accessibility (images only)" },
        "caption": { "type": "string", "description": "Optional caption text" },
        "created": { "type": "integer", "description": "Unix timestamp when media was added" }
      },
      "required": ["id", "type", "filename", "mimeType"]
    },

    "annotation": {
      "type": "object",
      "description": "Inline annotation within text",
      "properties": {
        "type": {
          "type": "string",
          "enum": ["footnote", "scripture", "strongs", "greek", "hebrew", "crossref", "abbrev", "page", "lexicon-ref", "media"]
        },
        "start": { "type": "integer", "description": "Character offset start position" },
        "end": { "type": "integer", "description": "Character offset end position" },
        "id": { "type": "string", "description": "Reference ID (footnote number, strongs key, etc.)" },
        "text": { "type": "string", "description": "The annotated text itself" },
        "implicit": { "type": "boolean", "description": "True if this was an implicit reference (e.g., 'v. 5' or '7:21, 28') rather than explicit (e.g., 'Rom 10:5')" },
        "data": {
          "type": "object",
          "description": "Additional data depending on annotation type",
          "properties": {
            "sv": { "type": "integer", "description": "Start verse ref (BBCCCVVV) for scripture annotations" },
            "ev": { "type": "integer", "description": "End verse ref (BBCCCVVV) for scripture ranges" },
            "refs": {
              "type": "array",
              "description": "Array of discrete verse references for comma-separated lists like '7:21, 28'",
              "items": {
                "type": "object",
                "properties": {
                  "sv": { "type": "integer", "description": "Start verse ref (BBCCCVVV)" },
                  "ev": { "type": "integer", "description": "End verse ref (BBCCCVVV) for ranges within the list" }
                },
                "required": ["sv"]
              }
            },
            "strongs": { "type": "string", "description": "Strong's number (e.g., G1080, H1234)" },
            "lexiconId": { "type": "string", "description": "Cross-reference to another lexicon entry (e.g., BDB123)" },
            "transliteration": { "type": "string" },
            "expansion": { "type": "string", "description": "Expanded form of abbreviation" },
            "pageNum": { "type": "string" },
            "mediaId": { "type": "string", "description": "Reference to media item in top-level media array (for media annotations)" },
            "mediaType": { "type": "string", "enum": ["image", "audio"], "description": "Type of media (for media annotations)" },
            "caption": { "type": "string", "description": "Optional caption for media" },
            "alignment": { "type": "string", "enum": ["left", "center", "right", "full"], "description": "Image alignment (for image media)" }
          }
        }
      },
      "required": ["type", "start", "end"]
    },

    "annotatedText": {
      "type": "object",
      "description": "Text content with annotations. Inline footnote numbers are stripped from the text for clean full-text search.",
      "properties": {
        "text": { "type": "string", "description": "Plain text content (markers stripped for searchability)" },
        "annotations": {
          "type": "array",
          "items": { "$ref": "#/definitions/annotation" }
        },
        "footnote_refs": {
          "type": "array",
          "description": "References to footnotes, with positions in the clean text where they appear",
          "items": {
            "type": "object",
            "properties": {
              "id": { "type": "string", "description": "Footnote ID (matches footnotes[].id)" },
              "offset": { "type": "integer", "description": "Character offset in the clean text where this footnote reference belongs" }
            },
            "required": ["id", "offset"]
          }
        }
      },
      "required": ["text"]
    },

    "footnote": {
      "type": "object",
      "properties": {
        "id": { "type": "string", "description": "Footnote identifier (typically a number)" },
        "content": { "$ref": "#/definitions/annotatedText" }
      },
      "required": ["id", "content"]
    },

    "verseRef": {
      "type": "integer",
      "description": "Numeric verse reference in BBCCCVVV format (book 2 digits, chapter 3 digits, verse 3 digits). E.g., 40001001 = Matt 1:1, 01001001 = Gen 1:1"
    },

    "sense": {
      "type": "object",
      "description": "A distinct meaning or usage of a dictionary entry",
      "properties": {
        "id": { "type": "integer", "description": "Sense number within this entry" },
        "partOfSpeech": { "type": "string", "description": "Grammatical category (noun, verb, etc.)" },
        "shortDefinition": {
          "oneOf": [
            { "type": "string" },
            { "$ref": "#/definitions/annotatedText" }
          ],
          "description": "Brief gloss or summary definition"
        },
        "definition": {
          "oneOf": [
            { "type": "string" },
            { "$ref": "#/definitions/annotatedText" }
          ],
          "description": "Full definition with usage examples"
        },
        "derivation": {
          "oneOf": [
            { "type": "string" },
            { "$ref": "#/definitions/annotatedText" }
          ],
          "description": "Etymology or word derivation"
        },
        "usage": { "type": "string", "description": "Usage notes" },
        "gloss": { "type": "string", "description": "Simple one-word gloss" },
        "references": {
          "type": "array",
          "description": "Bible verses where this sense occurs",
          "items": {
            "type": "object",
            "properties": {
              "sv": { "$ref": "#/definitions/verseRef" },
              "ev": { "$ref": "#/definitions/verseRef" }
            },
            "required": ["sv"]
          }
        },
        "translationUsages": {
          "type": "array",
          "description": "Word translation usage counts from Bible versions",
          "items": {
            "type": "object",
            "properties": {
              "version": { "type": "string", "description": "Bible version abbreviation (KJV, ESV, etc.)" },
              "translations": {
                "type": "array",
                "items": {
                  "type": "object",
                  "properties": {
                    "word": { "type": "string", "description": "English translation word" },
                    "count": { "type": "integer", "description": "Number of occurrences" }
                  },
                  "required": ["word"]
                }
              }
            },
            "required": ["version", "translations"]
          }
        }
      },
      "required": ["id"]
    },

    "dictionaryEntry": {
      "type": "object",
      "description": "A single dictionary/lexicon entry",
      "properties": {
        "key": { "type": "string", "description": "Entry key (e.g., H1234, G5678, BDB123)" },
        "lemma": { "type": "string", "description": "Hebrew/Greek word form" },
        "transliteration": { "type": "string", "description": "Romanized form" },
        "pronunciation": { "type": "string", "description": "Pronunciation guide" },
        "senses": {
          "type": "array",
          "items": { "$ref": "#/definitions/sense" },
          "description": "Different meanings/usages of the word"
        }
      },
      "required": ["key", "senses"]
    },

    "abbreviation": {
      "type": "object",
      "properties": {
        "abbrev": { "type": "string" },
        "expansion": { "type": "string" },
        "category": {
          "type": "string",
          "enum": ["publication", "bible", "apocrypha", "pseudepigrapha", "patristic", "classical", "qumran", "rabbinic", "general"]
        }
      },
      "required": ["abbrev", "expansion"]
    },

    "verseCommentary": {
      "type": "object",
      "description": "Commentary on a specific verse or verse range",
      "properties": {
        "sv": { "$ref": "#/definitions/verseRef", "description": "Start verse in BBCCCVVV format" },
        "ev": { "$ref": "#/definitions/verseRef", "description": "End verse (optional, for verse ranges)" },
        "suffix": { "type": "string", "description": "For partial verses like 'a', 'b'" },
        "translation": { "$ref": "#/definitions/annotatedText", "description": "Author's translation of the verse(s)" },
        "commentary": { "$ref": "#/definitions/annotatedText" },
        "footnotes": {
          "type": "array",
          "items": { "$ref": "#/definitions/footnote" }
        }
      },
      "required": ["sv"]
    },

    "pericope": {
      "type": "object",
      "description": "A unit/paragraph of text (e.g., 1:1-17)",
      "properties": {
        "id": { "type": "string", "description": "Pericope identifier like '1:1-17'" },
        "title": { "type": "string" },
        "sv": { "$ref": "#/definitions/verseRef", "description": "Start verse of pericope" },
        "ev": { "$ref": "#/definitions/verseRef", "description": "End verse of pericope" },
        "introduction": { "$ref": "#/definitions/annotatedText", "description": "Section introduction before verse commentary" },
        "translation": { "$ref": "#/definitions/annotatedText", "description": "Full translation of the pericope" },
        "verses": {
          "type": "array",
          "items": { "$ref": "#/definitions/verseCommentary" }
        },
        "footnotes": {
          "type": "array",
          "items": { "$ref": "#/definitions/footnote" },
          "description": "Footnotes for this pericope (shared across verses)"
        }
      },
      "required": ["id"]
    },

    "section": {
      "type": "object",
      "description": "Major section of commentary (e.g., 'I. Introducing The Messiah')",
      "properties": {
        "id": { "type": "string" },
        "title": { "type": "string" },
        "level": { "type": "integer", "description": "Nesting level (1=top, 2=sub, etc.)" },
        "sv": { "$ref": "#/definitions/verseRef", "description": "Start verse of section" },
        "ev": { "$ref": "#/definitions/verseRef", "description": "End verse of section" },
        "introduction": { "$ref": "#/definitions/annotatedText" },
        "subsections": {
          "type": "array",
          "items": { "$ref": "#/definitions/section" }
        },
        "pericopae": {
          "type": "array",
          "items": { "$ref": "#/definitions/pericope" }
        }
      },
      "required": ["id", "title"]
    },

    "chapterCommentary": {
      "type": "object",
      "description": "Commentary for a single chapter",
      "properties": {
        "chapter": { "type": "integer" },
        "introduction": { "$ref": "#/definitions/annotatedText" },
        "sections": {
          "type": "array",
          "items": { "$ref": "#/definitions/section" },
          "description": "For complex commentaries with hierarchical sections"
        },
        "pericopae": {
          "type": "array",
          "items": { "$ref": "#/definitions/pericope" },
          "description": "For commentaries organized by passage units"
        },
        "verses": {
          "type": "array",
          "items": { "$ref": "#/definitions/verseCommentary" },
          "description": "For simple verse-by-verse commentaries (use this OR sections/pericopae)"
        },
        "footnotes": {
          "type": "array",
          "items": { "$ref": "#/definitions/footnote" },
          "description": "Chapter-level footnotes (for simple commentaries)"
        }
      },
      "required": ["chapter"]
    }
  },

  "type": "object",
  "properties": {
    "meta": {
      "type": "object",
      "description": "Module metadata - unified across dictionaries and commentaries",
      "properties": {
        "schemaVersion": { "type": "string", "description": "Schema version (2.0 for unified schema)" },
        "id": { "type": "string", "description": "Unique module identifier (e.g., wsd_hebrew, NIC_Matt)" },
        "type": { "type": "string", "enum": ["dictionary", "commentary"], "description": "Module type" },
        "name": { "type": "string", "description": "Full module name" },
        "description": { "type": "string", "description": "Module description" },
        "seriesAbbrev": { "type": "string", "description": "Short series identifier (e.g., NIC, CWSD)" },
        "seriesFull": { "type": "string", "description": "Full series name" },
        "title": { "type": "string", "description": "Volume title (for commentaries)" },
        "author": { "type": "string" },
        "editor": { "type": "string" },
        "publisher": { "type": "string" },
        "year": { "type": "integer" },
        "isbn": { "type": "string" },
        "keyType": { "type": "string", "description": "For dictionaries: strongs-hebrew, strongs-greek, bdb, etc." },
        "language": { "type": "string", "description": "Primary language: hebrew, greek, english" }
      },
      "required": ["schemaVersion", "id", "type", "name"]
    },

    "abbreviations": {
      "type": "array",
      "items": { "$ref": "#/definitions/abbreviation" },
      "description": "List of abbreviations used in this module"
    },

    "entries": {
      "type": "array",
      "items": { "$ref": "#/definitions/dictionaryEntry" },
      "description": "Dictionary entries (for type=dictionary)"
    },

    "book": {
      "type": "string",
      "description": "Bible book abbreviation (for type=commentary)"
    },

    "bookNumber": {
      "type": "integer",
      "description": "Canonical book number 1-66 (for type=commentary)"
    },

    "frontMatter": {
      "type": "object",
      "description": "Front matter content (for type=commentary)",
      "properties": {
        "dedication": { "type": "string" },
        "editorPreface": { "$ref": "#/definitions/annotatedText" },
        "authorPreface": { "$ref": "#/definitions/annotatedText" },
        "introduction": { "$ref": "#/definitions/annotatedText" },
        "bibliography": {
          "type": "array",
          "items": {
            "type": "object",
            "properties": {
              "subsection": { "type": "string", "description": "Category like 'Commentaries', 'Books', 'Articles'" },
              "content": { "$ref": "#/definitions/annotatedText" }
            },
            "required": ["subsection", "content"]
          }
        }
      }
    },

    "chapters": {
      "type": "array",
      "items": { "$ref": "#/definitions/chapterCommentary" },
      "description": "Chapter commentaries (for type=commentary)"
    },

    "media": {
      "type": "array",
      "description": "Media files referenced by annotations in this module",
      "items": { "$ref": "#/definitions/mediaReference" }
    },

    "indices": {
      "type": "object",
      "description": "Optional indices for quick lookup",
      "properties": {
        "subjects": { "type": "object", "additionalProperties": { "type": "array", "items": { "type": "string" } } },
        "authors": { "type": "object", "additionalProperties": { "type": "array", "items": { "type": "string" } } },
        "scriptures": { "type": "object", "additionalProperties": { "type": "array", "items": { "type": "string" } } },
        "greekWords": { "type": "object", "additionalProperties": { "type": "array", "items": { "type": "string" } } },
        "hebrewWords": { "type": "object", "additionalProperties": { "type": "array", "items": { "type": "string" } } }
      }
    }
  },
  "required": ["meta"],
  "allOf": [
    {
      "if": { "properties": { "meta": { "properties": { "type": { "const": "dictionary" } } } } },
      "then": { "required": ["meta", "entries"] }
    },
    {
      "if": { "properties": { "meta": { "properties": { "type": { "const": "commentary" } } } } },
      "then": { "required": ["meta", "book", "bookNumber", "chapters"] }
    }
  ]
}
