"""Tests for LLM integration."""

from quran_validator import LLMProcessor, SYSTEM_PROMPTS, quick_validate
from quran_validator.types import LLMProcessorOptions


class TestLLMProcessor:
    def setup_method(self):
        self.processor = LLMProcessor()

    def test_process_valid_xml_tagged_quote(self):
        text = 'Here is a verse: <quran ref="1:1">بِسْمِ ٱللَّهِ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ</quran>'
        result = self.processor.process(text)
        assert len(result.quotes) >= 1
        assert result.quotes[0].is_valid is True
        assert result.quotes[0].detection_method == "tagged"

    def test_process_invalid_quote(self):
        text = '<quran ref="1:1">بسم الله الكريم</quran>'
        result = self.processor.process(text)
        assert len(result.quotes) >= 1
        assert result.quotes[0].is_valid is False

    def test_auto_correct_normalized_quote(self):
        processor = LLMProcessor(LLMProcessorOptions(auto_correct=True))
        # Use a common Arabic spelling instead of Uthmani
        text = '<quran ref="112:1">قل هو الله أحد</quran>'
        result = processor.process(text)
        assert len(result.quotes) >= 1
        # Should auto-correct to Uthmani text
        if result.quotes[0].was_corrected:
            assert result.quotes[0].corrected != result.quotes[0].original

    def test_system_prompts_available(self):
        assert "xml" in SYSTEM_PROMPTS
        assert "markdown" in SYSTEM_PROMPTS
        assert "bracket" in SYSTEM_PROMPTS
        assert "minimal" in SYSTEM_PROMPTS

    def test_get_system_prompt(self):
        prompt = self.processor.get_system_prompt()
        assert "quran" in prompt.lower()

    def test_inline_reference(self):
        text = "بِسْمِ ٱللَّهِ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ (1:1)"
        result = self.processor.process(text)
        assert len(result.quotes) >= 1

    def test_bracket_format(self):
        processor = LLMProcessor(LLMProcessorOptions(tag_format="bracket"))
        text = "[[Q:1:1|بِسْمِ ٱللَّهِ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ]]"
        result = processor.process(text)
        assert len(result.quotes) >= 1
        assert result.quotes[0].detection_method == "tagged"

    def test_verse_range(self):
        text = '<quran ref="112:1-4">قُلْ هُوَ ٱللَّهُ أَحَدٌ ٱللَّهُ ٱلصَّمَدُ لَمْ يَلِدْ وَلَمْ يُولَدْ وَلَمْ يَكُن لَّهُۥ كُفُوًا أَحَدٌۢ</quran>'
        result = self.processor.process(text)
        assert len(result.quotes) >= 1
        assert result.quotes[0].reference == "112:1-4"


class TestQuickValidate:
    def test_valid_content(self):
        text = '<quran ref="1:1">بِسْمِ ٱللَّهِ ٱلرَّحْمَـٰنِ ٱلرَّحِيمِ</quran>'
        result = quick_validate(text)
        assert result["has_quran_content"] is True

    def test_no_quran_content(self):
        text = "This is just regular English text."
        result = quick_validate(text)
        assert result["has_quran_content"] is False
