"""Tests para t (translate)."""

import sys
import os

sys.path.insert(0, os.path.dirname(__file__))

from t import t, _set_translations, _set_locale

TRANSLATIONS = {
    "en": {
        "report": {
            "sectionStart": "Generating section: {title}",
            "done": "Done",
            "deep": {
                "nested": {
                    "value": "Deep value"
                }
            }
        },
        "greeting": "Hello"
    },
    "es": {
        "report": {
            "done": "Listo"
        }
    }
}


def setup():
    _set_translations(TRANSLATIONS, default_locale="en")


def test_key_existente_retorna_traduccion():
    setup()
    result = t("report.done", locale="en")
    assert result == "Done", f"Expected 'Done', got '{result}'"


def test_key_inexistente_retorna_la_key():
    setup()
    result = t("nonexistent.key", locale="en")
    assert result == "nonexistent.key", f"Expected 'nonexistent.key', got '{result}'"


def test_interpolacion_de_variables():
    setup()
    result = t("report.sectionStart", locale="en", title="Introduction")
    assert result == "Generating section: Introduction", f"Got '{result}'"


def test_dot_path_profundo():
    setup()
    result = t("report.deep.nested.value", locale="en")
    assert result == "Deep value", f"Expected 'Deep value', got '{result}'"


def test_fallback_a_locale_default():
    setup()
    # "report.sectionStart" no existe en "es", debe hacer fallback a "en"
    result = t("report.sectionStart", locale="es", title="Intro")
    assert result == "Generating section: Intro", f"Got '{result}'"


if __name__ == "__main__":
    test_key_existente_retorna_traduccion()
    print("PASS: key existente retorna traduccion")

    test_key_inexistente_retorna_la_key()
    print("PASS: key inexistente retorna la key")

    test_interpolacion_de_variables()
    print("PASS: interpolacion de variables")

    test_dot_path_profundo()
    print("PASS: dot-path profundo")

    test_fallback_a_locale_default()
    print("PASS: fallback a locale default")

    print("---")
    print("All tests passed.")
