"""
Tests for services/quality/config.py
Covers the language → script mapping and discriminating characters.
"""
import pytest

from services.quality import config as qconfig


class TestGetScript:
    def test_cyrillic_languages(self):
        assert qconfig.get_script("ru") == "cyrillic"
        assert qconfig.get_script("uk") == "cyrillic"
        assert qconfig.get_script("be") == "cyrillic"
        assert qconfig.get_script("bg") == "cyrillic"
        assert qconfig.get_script("sr") == "cyrillic"
        assert qconfig.get_script("kk") == "cyrillic"

    def test_latin_languages(self):
        assert qconfig.get_script("en") == "latin"
        assert qconfig.get_script("fr") == "latin"
        assert qconfig.get_script("de") == "latin"
        assert qconfig.get_script("es") == "latin"
        assert qconfig.get_script("vi") == "latin"
        assert qconfig.get_script("tr") == "latin"

    def test_cjk_languages(self):
        assert qconfig.get_script("zh") == "cjk"
        assert qconfig.get_script("zh-cn") == "cjk"
        assert qconfig.get_script("zh-tw") == "cjk"

    def test_japanese_uses_kana(self):
        # ja is mapped to hiragana_katakana specifically so it can be
        # distinguished from Chinese CJK.
        assert qconfig.get_script("ja") == "hiragana_katakana"

    def test_korean_uses_hangul(self):
        assert qconfig.get_script("ko") == "hangul"

    def test_arabic_script_languages(self):
        assert qconfig.get_script("ar") == "arabic"
        assert qconfig.get_script("fa") == "arabic"
        assert qconfig.get_script("ur") == "arabic"
        assert qconfig.get_script("ps") == "arabic"
        assert qconfig.get_script("ku") == "arabic"

    def test_hebrew_and_yiddish(self):
        assert qconfig.get_script("he") == "hebrew"
        # Yiddish uses Hebrew script, not Arabic
        assert qconfig.get_script("yi") == "hebrew"

    def test_thaana(self):
        # Maldivian (Dhivehi) uses Thaana script, not Arabic
        assert qconfig.get_script("dv") == "thaana"

    def test_indian_scripts(self):
        assert qconfig.get_script("hi") == "devanagari"
        assert qconfig.get_script("bn") == "bengali"
        assert qconfig.get_script("ta") == "tamil"
        assert qconfig.get_script("te") == "telugu"
        assert qconfig.get_script("gu") == "gujarati"
        assert qconfig.get_script("pa") == "gurmukhi"
        assert qconfig.get_script("ml") == "malayalam"
        assert qconfig.get_script("kn") == "kannada"
        assert qconfig.get_script("si") == "sinhala"

    def test_se_asian_scripts(self):
        assert qconfig.get_script("th") == "thai"
        assert qconfig.get_script("lo") == "lao"
        assert qconfig.get_script("my") == "burmese"
        assert qconfig.get_script("km") == "khmer"

    def test_other_scripts(self):
        assert qconfig.get_script("el") == "greek"
        assert qconfig.get_script("ka") == "georgian"
        assert qconfig.get_script("hy") == "armenian"
        assert qconfig.get_script("am") == "ethiopic"
        assert qconfig.get_script("bo") == "tibetan"

    def test_unknown_falls_back_to_latin(self):
        assert qconfig.get_script("xx") == "latin"
        assert qconfig.get_script("") == "latin"
        assert qconfig.get_script(None) == "latin"

    def test_case_insensitive(self):
        assert qconfig.get_script("FR") == "latin"
        assert qconfig.get_script("ZH-CN") == "cjk"
        assert qconfig.get_script("Ru") == "cyrillic"


class TestIsArabicScriptLang:
    def test_true_for_arabic_script_langs(self):
        assert qconfig.is_arabic_script_lang("ar") is True
        assert qconfig.is_arabic_script_lang("fa") is True
        assert qconfig.is_arabic_script_lang("ur") is True
        assert qconfig.is_arabic_script_lang("ckb") is True

    def test_false_for_non_arabic(self):
        assert qconfig.is_arabic_script_lang("fr") is False
        assert qconfig.is_arabic_script_lang("he") is False
        assert qconfig.is_arabic_script_lang("hi") is False
        assert qconfig.is_arabic_script_lang("en") is False

    def test_false_for_unknown(self):
        assert qconfig.is_arabic_script_lang("xx") is False
        assert qconfig.is_arabic_script_lang("") is False
        assert qconfig.is_arabic_script_lang(None) is False


class TestDiscriminatingChars:
    def test_persian_chars(self):
        chars = qconfig.get_discriminating_chars("fa")
        assert "پ" in chars  # peh
        assert "چ" in chars  # tcheh
        assert "ژ" in chars  # zheh
        assert "گ" in chars  # guaf

    def test_urdu_chars(self):
        chars = qconfig.get_discriminating_chars("ur")
        assert "ٹ" in chars  # tteh
        assert "ڈ" in chars  # ddal

    def test_unknown_lang_empty(self):
        assert qconfig.get_discriminating_chars("fr") == frozenset()
        assert qconfig.get_discriminating_chars("") == frozenset()
        assert qconfig.get_discriminating_chars(None) == frozenset()


class TestGetRanges:
    def test_latin_returns_empty(self):
        # Latin is the fallback — no ranges, means "anything matches".
        assert qconfig.get_ranges("latin") == []

    def test_cyrillic_ranges(self):
        ranges = qconfig.get_ranges("cyrillic")
        assert any(start == 0x0400 for start, _ in ranges)

    def test_cjk_ranges(self):
        ranges = qconfig.get_ranges("cjk")
        assert any(start == 0x4E00 for start, _ in ranges)

    def test_unknown_script_returns_empty(self):
        assert qconfig.get_ranges("mystery_script") == []
