"""Tests for ieum.i18n -- internationalization module.

Covers:
- Loading translations from locale files
- English translations exist for expected keys
- Missing key returns the key itself as fallback
- Placeholder formatting with keyword arguments
- Locale switching between 'en' and 'ko'
- get_supported_locales returns expected values
"""

import pytest

from ieum.i18n import (
    _translations,
    get_locale,
    get_supported_locales,
    get_translations_dict,
    set_locale,
    t,
)


# ---------------------------------------------------------------------------
# Translation loading
# ---------------------------------------------------------------------------


class TestTranslationLoading:
    """Test that translation files are loaded on import."""

    def test_translations_loaded(self):
        """At least one locale should be loaded."""
        assert len(_translations) > 0

    def test_english_locale_loaded(self):
        assert "en" in _translations

    def test_english_translations_not_empty(self):
        assert len(_translations["en"]) > 0


# ---------------------------------------------------------------------------
# Translation lookup (t function)
# ---------------------------------------------------------------------------


class TestTranslationLookup:
    """Test the t() translation function."""

    def setup_method(self):
        """Ensure English locale for consistent test results."""
        set_locale("en")

    def test_known_key_returns_value(self):
        result = t("welcome.title")
        assert result != "welcome.title"  # Should be the actual translation
        assert isinstance(result, str)
        assert len(result) > 0

    def test_missing_key_returns_key(self):
        result = t("nonexistent.key.that.does.not.exist")
        assert result == "nonexistent.key.that.does.not.exist"

    def test_placeholder_formatting(self):
        # approval.title_single has {tool_name} placeholder
        result = t("approval.title_single", tool_name="shell")
        assert "shell" in result

    def test_placeholder_batch_count(self):
        result = t("approval.title_batch", count=5)
        assert "5" in result

    def test_missing_placeholder_graceful(self):
        # If placeholder key is missing, should not crash
        result = t("approval.title_single")  # no tool_name kwarg
        assert isinstance(result, str)


# ---------------------------------------------------------------------------
# Locale management
# ---------------------------------------------------------------------------


class TestLocaleManagement:
    """Test locale switching and detection."""

    def test_get_supported_locales(self):
        locales = get_supported_locales()
        assert "en" in locales
        assert "ko" in locales

    def test_set_locale_en(self):
        set_locale("en")
        assert get_locale() == "en"

    def test_set_locale_ko(self):
        set_locale("ko")
        assert get_locale() == "ko"
        set_locale("en")  # restore

    def test_set_invalid_locale_defaults_to_en(self):
        set_locale("xx")
        assert get_locale() == "en"

    def test_locale_affects_translation(self):
        set_locale("en")
        en_result = t("welcome.title")
        # Just verify it returns a non-empty string
        assert isinstance(en_result, str)
        assert len(en_result) > 0
        set_locale("en")  # restore


# ---------------------------------------------------------------------------
# get_translations_dict
# ---------------------------------------------------------------------------


class TestGetTranslationsDict:
    """Test bulk translation dictionary retrieval."""

    def test_returns_dict(self):
        d = get_translations_dict("en")
        assert isinstance(d, dict)

    def test_en_dict_has_keys(self):
        d = get_translations_dict("en")
        assert "welcome.title" in d

    def test_nonexistent_locale_returns_empty(self):
        d = get_translations_dict("zz")
        assert d == {}

    def test_default_uses_current_locale(self):
        set_locale("en")
        d = get_translations_dict()
        assert "welcome.title" in d


# ---------------------------------------------------------------------------
# English translation key coverage
# ---------------------------------------------------------------------------


class TestEnglishKeysCoverage:
    """Verify critical translation keys exist in English."""

    CRITICAL_KEYS = [
        "welcome.title",
        "welcome.hints",
        "status.manual_approval",
        "status.auto_approve",
        "approval.title_single",
        "approval.title_batch",
        "approval.option_approve",
        "approval.option_reject",
        "tool.running",
        "tool.error",
        "tool.rejected",
        "error.prefix",
    ]

    @pytest.mark.parametrize("key", CRITICAL_KEYS)
    def test_critical_key_exists(self, key):
        en = _translations.get("en", {})
        assert key in en, f"Missing critical English translation: {key}"
