"""Teste pentru eticheta_scurta."""

import pytest

from app.web.labels import eticheta_scurta, eticheta_stare


def test_eticheta_scurta_pentru_fiecare_stare():
    """Fiecare stare cunoscuta intoarce eticheta scurta corecta (pill)."""
    cazuri = {
        "queued":        "In coada",
        "sending":       "Se trimite",
        "sent":          "Finalizat",
        "needs_mapping": "De mapat",
        "needs_data":    "Date lipsa",
        "error":         "Eroare",
    }
    for status, eticheta_asteptata in cazuri.items():
        rezultat = eticheta_scurta(status)
        assert rezultat == eticheta_asteptata, (
            f"Status {status!r}: asteptam {eticheta_asteptata!r}, got {rezultat!r}"
        )


def test_eticheta_scurta_stare_necunoscuta_ridica_keyerror():
    """Stare neacoperita ridica KeyError (ca sa prinda stari noi adaugate in schema)."""
    with pytest.raises(KeyError):
        eticheta_scurta("stare_inexistenta")


def test_eticheta_lunga_ramane_pentru_subtext():
    """eticheta_stare inca intoarce textele lungi neschimbate (compat cu template-uri)."""
    cazuri_lungi = {
        "queued":        "In asteptare",
        "sending":       "Se trimite acum",
        "sent":          "Declarate la RAR",
        "needs_mapping": "Lipseste codul",
        "needs_data":    "Date incomplete",
        "error":         "Eroare la trimitere",
    }
    for status, fragment in cazuri_lungi.items():
        text, subtext, css_class = eticheta_stare(status)
        assert fragment.lower() in text.lower(), (
            f"eticheta_stare({status!r}) text lung modificat — asteptam {fragment!r} in {text!r}"
        )
        assert isinstance(css_class, str) and css_class, (
            f"eticheta_stare({status!r}) trebuie sa aiba css_class non-vida la pozitia 3"
        )


def test_eroare_bruta_prefera_cauza_tehnica():
    import json
    from app.web.labels import eroare_bruta
    rar_error = json.dumps({
        "cod": "RAR_EROARE_SERVER",
        "problema": "RAR a esuat la inregistrarea prezentarii",
        "cauza": "ORA-12899: value too large for column PRESTATII.COD",
        "fix": "…",
        "field": None,
        "message": "ORA-12899: value too large for column PRESTATII.COD",
    })
    assert eroare_bruta("error", rar_error) == (
        "ORA-12899: value too large for column PRESTATII.COD"
    )


def test_eroare_bruta_goala_pe_needs_mapping_si_stari_ok():
    from app.web.labels import eroare_bruta
    assert eroare_bruta("needs_mapping", '{"unmapped": [{"cod_op_service": "X"}]}') == ""
    assert eroare_bruta("sent", "orice") == ""
    assert eroare_bruta("queued", None) == ""


def test_eroare_bruta_validare_needs_data():
    import json
    from app.web.labels import eroare_bruta
    rar_error = json.dumps([{"field": "vin", "message": "VIN invalid (17 caractere)"}])
    assert eroare_bruta("needs_data", rar_error) == "VIN invalid (17 caractere)"


def test_eroare_bruta_nu_arunca_pe_json_corupt():
    from app.web.labels import eroare_bruta
    assert eroare_bruta("error", "{corupt") == "{corupt"
    assert eroare_bruta("error", None) == ""
