"""Test structural guard in _index_json_bible."""

import json
from pathlib import Path

import pytest

from accordance_parser.search_index import SearchIndex

# Mark as slow: this module imports SearchIndex and touches its internals.
pytestmark = pytest.mark.slow


def test__index_json_bible_skips_non_list_data(tmp_path: Path):
    """A JSON file that is a dict (not a list) should be skipped, returning 0."""
    si = SearchIndex(db_path=tmp_path / "test_guard.db")
    json_path = tmp_path / "some_morphology.json"
    json_path.write_text('{"key": "value", "data": {}}', encoding="utf-8")
    result = si._index_json_bible(json_path, "TEST", 999.0)
    assert result == 0, f"Expected 0 for dict JSON, got {result}"


def test__index_json_bible_skips_non_verse_list(tmp_path: Path):
    """A JSON file that is a list but lacks verse keys should be skipped."""
    si = SearchIndex(db_path=tmp_path / "test_guard2.db")
    json_path = tmp_path / "not_verses.json"
    json_path.write_text(
        json.dumps([{"id": 1, "name": "foo"}, {"id": 2, "name": "bar"}]), encoding="utf-8"
    )
    result = si._index_json_bible(json_path, "TEST2", 999.0)
    assert result == 0, f"Expected 0 for non-verse list, got {result}"


def test__index_json_bible_accepts_valid_verse_list(tmp_path: Path):
    """A valid verse list should be indexed normally (returns > 0)."""
    si = SearchIndex(db_path=tmp_path / "test_guard3.db")
    json_path = tmp_path / "valid_verses.json"
    verses = [
        {"book": 1, "chapter": 1, "verse": 1, "text": "In the beginning"},
        {"book": 1, "chapter": 1, "verse": 2, "text": "The earth was formless"},
    ]
    json_path.write_text(json.dumps(verses), encoding="utf-8")
    result = si._index_json_bible(json_path, "VALID", 999.0)
    assert result == 2, f"Expected 2 verses indexed, got {result}"
