"""Tests para load_translations."""

import json
import os
import sys
import tempfile
import shutil

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

from load_translations import load_translations


def test_carga_multiples_locales():
    tmp = tempfile.mkdtemp()
    try:
        with open(os.path.join(tmp, "en.json"), "w") as f:
            json.dump({"greeting": "Hello"}, f)
        with open(os.path.join(tmp, "es.json"), "w") as f:
            json.dump({"greeting": "Hola"}, f)

        result = load_translations(tmp)
        assert "en" in result, "Debe contener locale 'en'"
        assert "es" in result, "Debe contener locale 'es'"
        assert result["en"]["greeting"] == "Hello"
        assert result["es"]["greeting"] == "Hola"
    finally:
        shutil.rmtree(tmp)


def test_directorio_inexistente_retorna_dict_vacio():
    result = load_translations("/tmp/directorio_que_no_existe_xyz_12345")
    assert result == {}, f"Expected {{}}, got {result}"


def test_ignora_archivos_no_json():
    tmp = tempfile.mkdtemp()
    try:
        with open(os.path.join(tmp, "en.json"), "w") as f:
            json.dump({"key": "value"}, f)
        with open(os.path.join(tmp, "README.md"), "w") as f:
            f.write("# Locales")
        with open(os.path.join(tmp, "notes.txt"), "w") as f:
            f.write("some notes")

        result = load_translations(tmp)
        assert list(result.keys()) == ["en"], f"Expected only 'en', got {list(result.keys())}"
    finally:
        shutil.rmtree(tmp)


def test_locale_con_estructura_anidada():
    tmp = tempfile.mkdtemp()
    try:
        nested = {"report": {"sectionStart": "Section: {title}", "done": "Done"}}
        with open(os.path.join(tmp, "en.json"), "w") as f:
            json.dump(nested, f)

        result = load_translations(tmp)
        assert result["en"]["report"]["done"] == "Done"
        assert result["en"]["report"]["sectionStart"] == "Section: {title}"
    finally:
        shutil.rmtree(tmp)


if __name__ == "__main__":
    test_carga_multiples_locales()
    print("PASS: carga multiples locales")

    test_directorio_inexistente_retorna_dict_vacio()
    print("PASS: directorio inexistente retorna dict vacio")

    test_ignora_archivos_no_json()
    print("PASS: ignora archivos no json")

    test_locale_con_estructura_anidada()
    print("PASS: locale con estructura anidada")

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