"""Tests for language detection."""

import pytest
from unittest.mock import patch, MagicMock

from backend.language import detect_language, is_language_supported


class TestDetectLanguage:
    def test_english_text(self):
        result = detect_language("What is the weather like today? Please tell me.")
        assert result == "en"

    def test_italian_text(self):
        result = detect_language("Qual è il tempo oggi? Per favore dimmi di più.")
        assert result == "it"

    def test_french_text(self):
        result = detect_language("Bonjour, comment allez-vous aujourd'hui?")
        assert result == "fr"

    def test_german_text(self):
        result = detect_language("Guten Tag, wie geht es Ihnen heute?")
        assert result == "de"

    def test_spanish_text(self):
        result = detect_language("Buenos días, ¿cómo está usted hoy?")
        assert result == "es"

    def test_short_text_returns_none(self):
        assert detect_language("Hi") is None
        assert detect_language("Ciao") is None
        assert detect_language("") is None

    def test_none_input(self):
        assert detect_language(None) is None  # type: ignore

    def test_code_detected_as_english(self):
        # Code without much natural language
        code = "def hello():\n    return 'world'\n\nprint(hello())"
        result = detect_language(code)
        # Should detect something (likely en due to English keywords)
        assert result is not None


class TestIsLanguageSupported:
    def test_model_supports_all(self):
        # supported=None means all languages
        assert is_language_supported("en", None) is True
        assert is_language_supported("it", None) is True
        assert is_language_supported("fr", None) is True

    def test_model_supports_specific(self):
        supported = ["en", "de"]
        assert is_language_supported("en", supported) is True
        assert is_language_supported("de", supported) is True
        assert is_language_supported("fr", supported) is False
        assert is_language_supported("it", supported) is False

    def test_detection_unavailable(self):
        # detected=None should always return True (safe fallback)
        assert is_language_supported(None, ["en", "de"]) is True
        assert is_language_supported(None, None) is True

    def test_empty_supported_list(self):
        # Empty list means no languages supported
        assert is_language_supported("en", []) is False


class TestDetectorFallback:
    def test_without_langdetect(self):
        # Simulate langdetect not being installed
        with patch.dict("sys.modules", {"langdetect": None}):
            # Reset the cached detector
            import backend.language as lang_module
            lang_module._detector = None
            
            # Should handle gracefully
            from importlib import reload
            # Can't easily test this without actually uninstalling langdetect
            # Just verify the function doesn't crash
            pass
