from typing import Optional
from modules.i18n.locale_manager import DEFAULT_LOCALE, LocaleManager


class FallbackResolver:
    """Computes the prioritized fallback chain for a given request context."""

    @classmethod
    def get_candidate_locales(
        cls,
        user_locale: Optional[str] = None,
        bot_locale: Optional[str] = None,
        tenant_locale: Optional[str] = None,
    ) -> list[str]:
        candidates: list[str] = []

        # 1. User selected language
        if user_locale and LocaleManager.is_valid_locale(user_locale):
            candidates.append(user_locale)

        # 2. Bot default language
        if bot_locale and LocaleManager.is_valid_locale(bot_locale):
            if bot_locale not in candidates:
                candidates.append(bot_locale)

        # 3. Tenant default language
        if tenant_locale and LocaleManager.is_valid_locale(tenant_locale):
            if tenant_locale not in candidates:
                candidates.append(tenant_locale)

        # 4. Global default (English)
        if DEFAULT_LOCALE not in candidates:
            candidates.append(DEFAULT_LOCALE)

        return candidates
