"""get_lang and t(): language from config (DEFAULT_LANGUAGE), translate by key with fallback to en."""

from typing import TYPE_CHECKING

import duty_teller.config as config
from duty_teller.i18n.messages import MESSAGES

if TYPE_CHECKING:
    from telegram import User


def get_lang(user: "User | None") -> str:
    """
    Return the application language: always config.DEFAULT_LANGUAGE.

    The user argument is kept for backward compatibility but is ignored.
    The whole deployment uses a single language from DEFAULT_LANGUAGE.
    """
    return config.DEFAULT_LANGUAGE


def t(lang: str, key: str, **kwargs: str) -> str:
    """
    Return translated string for lang and key; substitute kwargs into placeholders like {phone}.
    Fallback to 'en' if key missing for lang.
    """
    lang = "ru" if lang == "ru" else "en"
    messages = MESSAGES.get(lang) or MESSAGES["en"]
    template = messages.get(key)
    if template is None:
        template = MESSAGES["en"].get(key, key)
    try:
        return template.format(**kwargs)
    except KeyError:
        return template
