"""Tests for 12 Catholic Bible translator/linguist scenarios — Session 79.

Covers Mark 16:9-20 apparatus, John 7:53-8:11 Pericope Adulterae,
1 John 5:7-8 Comma Johanneum, Isaiah 7:14 LXX parthenos, John 21 agapao/phileo,
Romans 3 dikaiosyne theou, Genesis 1 waw-consecutive, Psalm 119 acrostic,
Isaiah 6 LXX/MT quotation, Romans 9:5 punctuation, diatheke consistency,
and Psalm 23 back-translation accuracy.

Each scenario (S-TR-01 through S-TR-12) maps to one test class with
multiple test methods validating that the tool chain returns non-None
results. Tests gracefully skip when dependencies are unavailable.
"""

from __future__ import annotations
import pytest


# =============================================================================
# IMPORT GUARDS — following test_session78_theologian.py conventions
# =============================================================================

# --- Bible API (network-dependent verse retrieval) ---
try:
    from accordance_parser.bible_api import get_verse
    _has_bible_api = True
except Exception:
    _has_bible_api = False

# --- Strong's tools ---
try:
    from accordance_services.tools.strongs import search_verses_by_strongs
    _has_strongs_tools = True
except Exception:
    _has_strongs_tools = False

# --- ACC2 tools (OT quotations) ---
try:
    from accordance_services.tools.acc2 import find_ot_quotations
    _has_acc2_tools = True
except Exception:
    _has_acc2_tools = False

# --- Multi-tools (verse range) ---
try:
    from accordance_services.tools.text import get_verse_range as mcp_get_verse_range
    _has_multi_tools = True
except Exception:
    _has_multi_tools = False

# --- Morphology ---
try:
    from accordance_parser.morphology import (
        get_morphology,
    )
    _has_morphology = True
except Exception:
    _has_morphology = False

# --- MCP server tools ---
_has_mcp_tools = False
_has_lexicons = False
_has_commentaries = False
_has_search_index = False
try:
    from accordance_mcp_server import (
        study_strongs_number,
        search_commentaries,
        compare_translations,
        find_cross_references,
        search_all,
        interlinear_display,
        get_strongs_for_verse,
        word_study,
    )
    _has_mcp_tools = True

    import accordance_mcp_server as _mcp_srv
    if getattr(_mcp_srv, "_lexicon_modules", None) and len(_mcp_srv._lexicon_modules) > 0:
        _has_lexicons = True
    if getattr(_mcp_srv, "_commentary_modules", None) and len(_mcp_srv._commentary_modules) > 0:
        _has_commentaries = True
    if getattr(_mcp_srv, "_search_index", None) is not None:
        _has_search_index = True
except Exception:
    pass


# --- Network detection ---
_has_network = False
if _has_bible_api:
    try:
        r = get_verse("Genesis 1:1", "KJV")
        if r and r.text:
            _has_network = True
    except Exception:
        pass


# =============================================================================
# S-TR-01: Mark 16:9-20 NA28 Apparatus Consultation
# =============================================================================

@pytest.mark.skipif(not _has_network, reason="Requires network")
class TestMark16_9_20Apparatus:
    """S-TR-01: Mark 16:9-20 longer ending — apparatus, translations, commentaries."""

    def test_get_verse_mark_16_9(self):
        """Retrieve Mark 16:9 in available translation (NABRE, ESV, or KJV)."""
        for trans in ["KJV", "ESV", "NABRE"]:
            vr = get_verse("Mark 16:9", trans)
            assert vr is not None
            assert len(vr.text) > 10

    def test_compare_translations_mark_16_9(self):
        """Compare ESV, NASB, NABRE, KJV for Mark 16:9 — observe bracketing."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = compare_translations("Mark 16:9", "ESV,NASB,NABRE,KJV")
        assert result is not None
        assert isinstance(result, str) and len(result) > 20

    def test_search_commentaries_mark_16_9(self):
        """Search commentaries for Mark 16:9 — results exist."""
        if not _has_mcp_tools or not _has_commentaries:
            pytest.skip("Commentary tools not available")
        result = search_commentaries("Mark 16:9")
        assert result is not None
        assert isinstance(result, str)

    def test_fts5_longer_ending_of_mark(self):
        """FTS5 search: 'longer ending of Mark' across all modules."""
        if not _has_mcp_tools or not _has_search_index:
            pytest.skip("Search index not available")
        result = search_all("longer ending of Mark", max_results=5)
        assert result is not None
        assert isinstance(result, str)

    def test_find_cross_references_mark_16_9(self):
        """Cross references for Mark 16:9 — must not crash on disputed verse."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = find_cross_references("Mark 16:9")
        assert result is not None
        assert isinstance(result, str)


# =============================================================================
# S-TR-02: John 7:53-8:11 Pericope Adulterae
# =============================================================================

@pytest.mark.skipif(not _has_network, reason="Requires network")
class TestPericopeAdulterae:
    """S-TR-02: John 7:53-8:11 — textual variant across translations and commentaries."""

    def test_get_verse_john_8_7_nabre(self):
        """Retrieve John 8:7 in NABRE."""
        vr = get_verse("John 8:7", "NABRE")
        assert vr is not None
        assert len(vr.text) > 10

    def test_compare_translations_john_8_7(self):
        """Compare ESV, NASB, NABRE, KJV for John 8:7."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = compare_translations("John 8:7", "ESV,NASB,NABRE,KJV")
        assert result is not None
        assert isinstance(result, str) and len(result) > 20

    def test_search_commentaries_john_7_53(self):
        """Search commentaries for John 7:53 — commentary acknowledges textual issue."""
        if not _has_mcp_tools or not _has_commentaries:
            pytest.skip("Commentary tools not available")
        result = search_commentaries("John 7:53")
        assert result is not None
        assert isinstance(result, str)

    def test_fts5_pericope_adulterae(self):
        """FTS5 search: 'Pericope Adulterae' in commentaries."""
        if not _has_mcp_tools or not _has_search_index:
            pytest.skip("Search index not available")
        result = search_all("Pericope Adulterae", max_results=5)
        assert result is not None
        assert isinstance(result, str)


# =============================================================================
# S-TR-03: 1 John 5:7-8 Comma Johanneum
# =============================================================================

@pytest.mark.skipif(not _has_network, reason="Requires network")
class TestCommaJohanneum:
    """S-TR-03: 1 John 5:7-8 — presence/absence of Comma across translations."""

    def test_get_verse_1john_5_7_kjv(self):
        """Retrieve 1 John 5:7 in KJV — includes Comma Johanneum."""
        vr = get_verse("1 John 5:7", "KJV")
        assert vr is not None
        assert len(vr.text) > 10

    def test_compare_translations_1john_5_7(self):
        """Compare KJV, ESV, NABRE for 1 John 5:7 — show presence/absence."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = compare_translations("1 John 5:7", "KJV,ESV,NABRE")
        assert result is not None
        assert isinstance(result, str) and len(result) > 20

    def test_search_commentaries_1john_5_7(self):
        """Search commentaries for 1 John 5:7."""
        if not _has_mcp_tools or not _has_commentaries:
            pytest.skip("Commentary tools not available")
        result = search_commentaries("1 John 5:7")
        assert result is not None
        assert isinstance(result, str)

    def test_find_cross_references_1john_5_7(self):
        """Cross references for 1 John 5:7."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = find_cross_references("1 John 5:7")
        assert result is not None
        assert isinstance(result, str)


# =============================================================================
# S-TR-04: Isaiah 7:14 LXX parthenos vs MT almah
# =============================================================================

@pytest.mark.skipif(not _has_network, reason="Requires network")
class TestIsaiah7_14Parthenos:
    """S-TR-04: Isaiah 7:14 — young woman (almah) vs virgin (parthenos) across translations."""

    def test_get_verse_isaiah_7_14_nabre(self):
        """Retrieve Isaiah 7:14 in NABRE."""
        vr = get_verse("Isaiah 7:14", "NABRE")
        assert vr is not None
        assert len(vr.text) > 10

    def test_compare_translations_isaiah_7_14(self):
        """Compare NABRE, RSVCE, KJV, ESV for Isaiah 7:14 — young woman vs virgin."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = compare_translations("Isaiah 7:14", "NABRE,RSVCE,KJV,ESV")
        assert result is not None
        assert isinstance(result, str) and len(result) > 20

    def test_study_strongs_h5959(self):
        """Study Strong's H5959 (almah) if available."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = study_strongs_number("H5959")
        assert result is not None
        assert isinstance(result, str) and len(result) > 20

    def test_find_ot_quotations_matthew_1_23(self):
        """Find OT quotations for Matthew 1:23 — verify Isaiah 7:14 chain."""
        if not _has_acc2_tools:
            pytest.skip("ACC2 tools not available")
        result = find_ot_quotations("Matthew 1:23")
        assert result is not None
        assert isinstance(result, str)

    def test_search_commentaries_isaiah_7_14(self):
        """Search commentaries for Isaiah 7:14."""
        if not _has_mcp_tools or not _has_commentaries:
            pytest.skip("Commentary tools not available")
        result = search_commentaries("Isaiah 7:14")
        assert result is not None
        assert isinstance(result, str)


# =============================================================================
# S-TR-05: John 21:15-17 agapao vs phileo distinction
# =============================================================================

@pytest.mark.skipif(not _has_network, reason="Requires network")
class TestAgapaoPhileo:
    """S-TR-05: John 21:15-17 — agapao/phileo distinction in restoration dialogue."""

    def test_get_verse_john_21_15_17_nabre(self):
        """Retrieve John 21:15-17 in NABRE."""
        vr = get_verse("John 21:15-17", "NABRE")
        assert vr is not None
        assert len(vr.text) > 10

    def test_get_morphology_john_21_15(self):
        """Get morphology for John 21:15 — present active indicative forms."""
        if not _has_morphology:
            pytest.skip("Morphology not available")
        entries = get_morphology("John 21:15")
        assert isinstance(entries, list)

    def test_search_commentaries_john_21_15(self):
        """Search commentaries for John 21:15 — agapao/phileo discussion."""
        if not _has_mcp_tools or not _has_commentaries:
            pytest.skip("Commentary tools not available")
        result = search_commentaries("John 21:15")
        assert result is not None
        assert isinstance(result, str)

    def test_fts5_agapao_phileo(self):
        """FTS5 search: 'agapao phileo John 21' across modules."""
        if not _has_mcp_tools or not _has_search_index:
            pytest.skip("Search index not available")
        result = search_all("agapao phileo John 21", max_results=5)
        assert result is not None
        assert isinstance(result, str)

    def test_interlinear_john_21_15(self):
        """Interlinear display for John 21:15 if available."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = interlinear_display("John 21:15")
        assert result is not None
        assert isinstance(result, str)


# =============================================================================
# S-TR-06: Romans 3:21-26 dikaiosyne theou translation
# =============================================================================

@pytest.mark.skipif(not _has_network, reason="Requires network")
class TestDikaiosyneTheou:
    """S-TR-06: Romans 3:21-26 — righteousness of God (dikaiosyne theou) translation."""

    def test_get_verse_romans_3_21_22_nabre(self):
        """Retrieve Romans 3:21-22 in NABRE."""
        vr = get_verse("Romans 3:21-22", "NABRE")
        assert vr is not None
        assert len(vr.text) > 10

    def test_compare_translations_romans_3_22(self):
        """Compare NASB, ESV, NABRE, NIV, KJV for Romans 3:22."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = compare_translations("Romans 3:22", "NASB,ESV,NABRE,NIV,KJV")
        assert result is not None
        assert isinstance(result, str) and len(result) > 20

    def test_get_strongs_romans_3_22(self):
        """Get Strong's numbers for Romans 3:22 — includes G1343, G4102."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = get_strongs_for_verse("Romans 3:22", "KJV")
        assert result is not None
        assert isinstance(result, str) and len(result) > 10

    def test_study_strongs_g1343(self):
        """Study Strong's G1343 (dikaiosyne) if available."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = study_strongs_number("G1343")
        assert result is not None
        assert isinstance(result, str) and len(result) > 20

    def test_search_commentaries_romans_3_21(self):
        """Search commentaries for Romans 3:21."""
        if not _has_mcp_tools or not _has_commentaries:
            pytest.skip("Commentary tools not available")
        result = search_commentaries("Romans 3:21")
        assert result is not None
        assert isinstance(result, str)


# =============================================================================
# S-TR-07: Genesis 1:3-5 Hebrew waw-consecutive chain
# =============================================================================

@pytest.mark.skipif(not _has_network, reason="Requires network")
class TestWawConsecutiveChain:
    """S-TR-07: Genesis 1:3-5 — Hebrew waw-consecutive (vayyiqtol) chain."""

    def test_get_verse_genesis_1_3(self):
        """Retrieve Genesis 1:3 in KJV or ESV."""
        for trans in ["KJV", "ESV"]:
            vr = get_verse("Genesis 1:3", trans)
            assert vr is not None
            assert len(vr.text) > 5

    def test_get_morphology_genesis_1_3(self):
        """Get morphology for Genesis 1:3 — waw-consecutive parsing."""
        if not _has_morphology:
            pytest.skip("Morphology not available")
        entries = get_morphology("Genesis 1:3")
        assert isinstance(entries, list)

    def test_get_morphology_genesis_1_4(self):
        """Get morphology for Genesis 1:4 — chain continuity."""
        if not _has_morphology:
            pytest.skip("Morphology not available")
        entries = get_morphology("Genesis 1:4")
        assert isinstance(entries, list)

    def test_get_morphology_genesis_1_5(self):
        """Get morphology for Genesis 1:5 — chain continuity."""
        if not _has_morphology:
            pytest.skip("Morphology not available")
        entries = get_morphology("Genesis 1:5")
        assert isinstance(entries, list)

    def test_get_verse_range_genesis_1_3_5(self):
        """Retrieve verse range Genesis 1:3-5 if available."""
        if not _has_multi_tools:
            pytest.skip("Multi-tools not available")
        result = mcp_get_verse_range("KJVS", "Genesis 1:3", "Genesis 1:5")
        assert result is not None
        assert isinstance(result, str)


# =============================================================================
# S-TR-08: Psalm 119 acrostic structure + LXX/MT mapping
# =============================================================================

@pytest.mark.skipif(not _has_network, reason="Requires network")
class TestPsalm119Acrostic:
    """S-TR-08: Psalm 119 — acrostic structure and LXX/MT mapping."""

    def test_get_verse_psalm_119_1_nabre(self):
        """Retrieve Psalm 119:1 in NABRE."""
        vr = get_verse("Psalm 119:1", "NABRE")
        assert vr is not None
        assert len(vr.text) > 10

    def test_get_verse_psalm_119_105_nabre(self):
        """Retrieve Psalm 119:105 in NABRE — lamp to my feet."""
        vr = get_verse("Psalm 119:105", "NABRE")
        assert vr is not None
        assert len(vr.text) > 10

    def test_compare_translations_psalm_119_105(self):
        """Compare ESV, NASB, NABRE, KJV for Psalm 119:105."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = compare_translations("Psalm 119:105", "ESV,NASB,NABRE,KJV")
        assert result is not None
        assert isinstance(result, str) and len(result) > 20

    def test_find_cross_references_psalm_119_105(self):
        """Cross references for Psalm 119:105."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = find_cross_references("Psalm 119:105")
        assert result is not None
        assert isinstance(result, str)

    def test_word_study_psalm_119_105(self):
        """Word study on Psalm 119:105 if available."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = word_study("Psalm 119:105")
        assert result is not None
        assert isinstance(result, str) and len(result) > 10


# =============================================================================
# S-TR-09: Isaiah 6:9-10 LXX/MT + NT quotation chain
# =============================================================================

@pytest.mark.skipif(not _has_network, reason="Requires network")
class TestIsaiah6_9_10QuotationChain:
    """S-TR-09: Isaiah 6:9-10 — LXX/MT hardening and NT quotation in Matthew 13."""

    def test_get_verse_isaiah_6_9_nabre(self):
        """Retrieve Isaiah 6:9 in NABRE."""
        vr = get_verse("Isaiah 6:9", "NABRE")
        assert vr is not None
        assert len(vr.text) > 10

    def test_compare_translations_isaiah_6_9(self):
        """Compare NABRE, ESV, KJV for Isaiah 6:9."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = compare_translations("Isaiah 6:9", "NABRE,ESV,KJV")
        assert result is not None
        assert isinstance(result, str) and len(result) > 20

    def test_get_verse_matthew_13_14_nabre(self):
        """Retrieve Matthew 13:14 in NABRE — NT quotation of Isaiah 6."""
        vr = get_verse("Matthew 13:14", "NABRE")
        assert vr is not None
        assert len(vr.text) > 10

    def test_find_ot_quotations_matthew_13_14(self):
        """Find OT quotations for Matthew 13:14 if acc2 available."""
        if not _has_acc2_tools:
            pytest.skip("ACC2 tools not available")
        result = find_ot_quotations("Matthew 13:14")
        assert result is not None
        assert isinstance(result, str)

    def test_fts5_isaiah_hardening_heart(self):
        """FTS5 search: 'Isaiah hardening heart' if available."""
        if not _has_mcp_tools or not _has_search_index:
            pytest.skip("Search index not available")
        result = search_all("Isaiah hardening heart", max_results=5)
        assert result is not None
        assert isinstance(result, str)


# =============================================================================
# S-TR-10: Romans 9:5 punctuation/Christological doxology
# =============================================================================

@pytest.mark.skipif(not _has_network, reason="Requires network")
class TestRomans9_5Doxology:
    """S-TR-10: Romans 9:5 — punctuation ambiguity and Christological doxology."""

    def test_get_verse_romans_9_5_nabre(self):
        """Retrieve Romans 9:5 in NABRE."""
        vr = get_verse("Romans 9:5", "NABRE")
        assert vr is not None
        assert len(vr.text) > 10

    def test_get_morphology_romans_9_5(self):
        """Get morphology for Romans 9:5 if available."""
        if not _has_morphology:
            pytest.skip("Morphology not available")
        entries = get_morphology("Romans 9:5")
        assert isinstance(entries, list)

    def test_compare_translations_romans_9_5(self):
        """Compare ESV, NASB, NABRE, NRSV, KJV for Romans 9:5 — punctuation differences."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = compare_translations("Romans 9:5", "ESV,NASB,NABRE,NRSV,KJV")
        assert result is not None
        assert isinstance(result, str) and len(result) > 20

    def test_search_commentaries_romans_9_5(self):
        """Search commentaries for Romans 9:5."""
        if not _has_mcp_tools or not _has_commentaries:
            pytest.skip("Commentary tools not available")
        result = search_commentaries("Romans 9:5")
        assert result is not None
        assert isinstance(result, str)


# =============================================================================
# S-TR-11: diatheke (covenant/testament) consistency across Hebrews
# =============================================================================

@pytest.mark.skipif(not _has_network, reason="Requires network")
class TestDiathekeConsistency:
    """S-TR-11: diatheke — covenant vs testament consistency in Hebrews 9."""

    def test_get_verse_hebrews_9_15_nabre(self):
        """Retrieve Hebrews 9:15 in NABRE."""
        vr = get_verse("Hebrews 9:15", "NABRE")
        assert vr is not None
        assert len(vr.text) > 10

    def test_study_strongs_g1242(self):
        """Study Strong's G1242 (diatheke) — frequency/distribution if available."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = study_strongs_number("G1242")
        assert result is not None
        assert isinstance(result, str) and len(result) > 20

    def test_search_verses_by_strongs_g1242(self):
        """Search verses by Strong's G1242 if available."""
        if not _has_strongs_tools:
            pytest.skip("Strong's tools not available")
        result = search_verses_by_strongs("G1242", max_results=50)
        assert result is not None
        assert isinstance(result, str) and len(result) > 10

    def test_compare_translations_hebrews_9_16(self):
        """Compare NASB, ESV, NABRE, NIV, KJV for Hebrews 9:16."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = compare_translations("Hebrews 9:16", "NASB,ESV,NABRE,NIV,KJV")
        assert result is not None
        assert isinstance(result, str) and len(result) > 20

    def test_search_commentaries_hebrews_9_15(self):
        """Search commentaries for Hebrews 9:15."""
        if not _has_mcp_tools or not _has_commentaries:
            pytest.skip("Commentary tools not available")
        result = search_commentaries("Hebrews 9:15")
        assert result is not None
        assert isinstance(result, str)


# =============================================================================
# S-TR-12: Psalm 23 back-translation accuracy check
# =============================================================================

@pytest.mark.skipif(not _has_network, reason="Requires network")
class TestPsalm23BackTranslation:
    """S-TR-12: Psalm 23 — back-translation accuracy across versions."""

    def test_get_verse_psalm_23_1_nabre(self):
        """Retrieve Psalm 23:1 in NABRE."""
        vr = get_verse("Psalm 23:1", "NABRE")
        assert vr is not None
        assert len(vr.text) > 5

    def test_get_morphology_psalm_23_1(self):
        """Get morphology for Psalm 23:1 if available."""
        if not _has_morphology:
            pytest.skip("Morphology not available")
        entries = get_morphology("Psalm 23:1")
        assert isinstance(entries, list)

    def test_get_strongs_psalm_23_1_kjv(self):
        """Get Strong's numbers for Psalm 23:1 KJV — includes H3068, H7462."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = get_strongs_for_verse("Psalm 23:1", "KJV")
        assert result is not None
        assert isinstance(result, str) and len(result) > 10

    def test_word_study_psalm_23_1(self):
        """Word study on Psalm 23:1 if available."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = word_study("Psalm 23:1")
        assert result is not None
        assert isinstance(result, str) and len(result) > 10

    def test_compare_translations_psalm_23_1(self):
        """Compare NASB, ESV, NABRE, NIV, KJV for Psalm 23:1."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = compare_translations("Psalm 23:1", "NASB,ESV,NABRE,NIV,KJV")
        assert result is not None
        assert isinstance(result, str) and len(result) > 20

    def test_interlinear_psalm_23_1(self):
        """Interlinear display for Psalm 23:1 if available."""
        if not _has_mcp_tools:
            pytest.skip("MCP tools not available")
        result = interlinear_display("Psalm 23:1")
        assert result is not None
        assert isinstance(result, str)
