"""Tests for Arabic text normalization."""

from quran_validator.normalizer import (
    normalize_arabic,
    remove_diacritics,
    contains_arabic,
    extract_arabic_segments,
    NormalizeOptions,
)


class TestNormalizeArabic:
    def test_remove_diacritics(self):
        result = normalize_arabic("السَّلَامُ عَلَيْكُمُ")
        assert result == "السلام عليكم"

    def test_preserve_hamza_forms_normalize_alef_variants(self):
        # أ إ preserved (hamza carriers), آ ٱ → ا
        result = normalize_arabic("أإآٱ")
        assert result == "أإاا"

    def test_preserve_alef_maqsura(self):
        result = normalize_arabic("على")
        assert result == "على"

    def test_preserve_teh_marbuta(self):
        result = normalize_arabic("رحمة")
        assert result == "رحمة"

    def test_remove_tatweel(self):
        result = normalize_arabic("كتـــاب")
        assert result == "كتاب"

    def test_normalize_whitespace(self):
        result = normalize_arabic("بسم   الله")
        assert result == "بسم الله"

    def test_handle_alef_wasla(self):
        result = normalize_arabic("ٱللَّهُ")
        assert result == "الله"

    def test_normalize_presentation_form_ligatures(self):
        # U+FDF2 ARABIC LIGATURE ALLAH ISOLATED FORM
        result = normalize_arabic("\uFDF2")
        assert result == "الله"

    def test_strip_bidi_controls_and_verse_numbers(self):
        input_text = "سورة \u200F١١٢:١ \u200Fقُلْ هُوَ ٱللَّهُ أَحَدٌ"
        result = normalize_arabic(input_text)
        assert "سورة" in result
        assert "قل هو الله أحد" in result
        assert "\u200c" not in result
        assert "\u200d" not in result
        assert "\u200e" not in result
        assert "\u200f" not in result
        assert "\u061c" not in result


class TestUthmaniNormalization:
    def test_alef_wasla_variant(self):
        uthmani = "ٱلصَّدَقَـٰتُ"
        result = normalize_arabic(uthmani)
        assert result == "الصدقات"

    def test_remove_rubul_hizb(self):
        with_rubul = "۞ إِنَّمَا"
        result = normalize_arabic(with_rubul)
        assert "۞" not in result
        assert result == "إنما"

    def test_superscript_alef_to_regular_alef(self):
        uthmani = "ٱلسَّمَٰوَٰتِ"
        result = normalize_arabic(uthmani)
        assert result == "السماوات"

    def test_no_extra_alef_for_hamza_before_alef(self):
        uthmani = "ٱلْـَٔاخِرِ"
        result = normalize_arabic(uthmani)
        assert result == "الاخر"


class TestRemoveDiacritics:
    def test_remove_all_diacritical_marks(self):
        result = remove_diacritics("بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ")
        assert "ِ" not in result
        assert "ْ" not in result
        assert "َ" not in result


class TestContainsArabic:
    def test_arabic_text(self):
        assert contains_arabic("مرحبا") is True
        assert contains_arabic("Hello مرحبا world") is True

    def test_non_arabic_text(self):
        assert contains_arabic("Hello world") is False
        assert contains_arabic("123") is False


class TestExtractArabicSegments:
    def test_extract_segments(self):
        segments = extract_arabic_segments("Say بسم الله and continue")
        assert len(segments) >= 1
        assert any("بسم الله" in s.text for s in segments)
