"""Teste reguli text + auto_send dupa US-001 (PRD 5.11).

Inainte de US-001: o regula text cu auto_send=0 tinea randul in needs_mapping
(regula_fara_autosend=True + has_no_auto_send -> True).

Dupa US-001: has_no_auto_send mereu False; o regula text cu auto_send=0 rezolva
codul RAR si randul intra in queued direct. regula_fara_autosend nu se mai seteaza.

Functii pure -> teste fara DB.
"""

from __future__ import annotations

from app.mapping import classify_prezentare, has_no_auto_send, resolve_prestatii


VALID = {"OE-2"}
_CONTENT = {
    "vin": "WDB123456789012AB",
    "nr_inmatriculare": "B123ABC",
    "data_prestatie": "2026-06-22",
    "odometru_final": "1000",
}


def _content_cu(op_denumire="Verificare faruri"):
    return {**_CONTENT, "prestatii": [{"cod_op_service": "X99", "denumire": op_denumire}]}


def test_regula_auto_send_0_acum_queued():
    """Regula text auto_send=0 + continut valid -> queued (nu mai needs_mapping) dupa US-001."""
    tr = [{"pattern": "verificare", "cod_prestatie": "OE-2", "auto_send": 0, "priority": 0}]
    cl = classify_prezentare(_content_cu(), {}, {}, VALID, tr)
    assert cl["status"] == "queued", f"asteptat queued (auto_send ignorat), got {cl['status']}"


def test_regula_auto_send_1_trece_in_coada():
    """Regula text auto_send=1 + continut valid -> queued (neschimbat)."""
    tr = [{"pattern": "verificare", "cod_prestatie": "OE-2", "auto_send": 1, "priority": 0}]
    cl = classify_prezentare(_content_cu(), {}, {}, VALID, tr)
    assert cl["status"] == "queued", f"asteptat queued, got {cl['status']}"


def test_has_no_auto_send_mereu_false_cu_flag_regula():
    """has_no_auto_send=False chiar daca regula_fara_autosend e prezent (US-001)."""
    tr = [{"pattern": "verificare", "cod_prestatie": "OE-2", "auto_send": 0, "priority": 0}]
    resolved, unmapped = resolve_prestatii(
        [{"cod_op_service": "X99", "denumire": "Verificare faruri"}], {}, VALID, tr
    )
    assert unmapped == []
    assert resolved[0]["cod_prestatie"] == "OE-2"
    # Flagul regula_fara_autosend nu se mai seteaza (US-001)
    assert resolved[0].get("regula_fara_autosend") is None, (
        "regula_fara_autosend nu trebuie sa mai fie setat dupa US-001"
    )
    assert has_no_auto_send(resolved, {}) is False


def test_has_no_auto_send_fals_cand_regula_auto_send_1():
    """Regula auto_send=1 -> fara flag -> has_no_auto_send False (neschimbat)."""
    tr = [{"pattern": "verificare", "cod_prestatie": "OE-2", "auto_send": 1, "priority": 0}]
    resolved, _ = resolve_prestatii(
        [{"cod_op_service": "X99", "denumire": "Verificare faruri"}], {}, VALID, tr
    )
    assert resolved[0].get("regula_fara_autosend") is None
    assert has_no_auto_send(resolved, {}) is False


def test_adnotari_stale_curatate_la_mapare_exacta():
    """Un item venit cu cod_sursa/regula_fara_autosend stale dar re-rezolvat acum prin
    mapare EXACTA -> adnotarile sunt curatate; randul NU mai e tinut (neschimbat)."""
    item_stale = {
        "cod_op_service": "X99",
        "denumire": "Verificare faruri",
        "cod_sursa": "text_rule:verificare",
        "regula_fara_autosend": True,
    }
    mapping = {"X99": "OE-2"}
    mapping_meta = {"X99": {"cod_prestatie": "OE-2", "auto_send": True}}
    resolved, _ = resolve_prestatii([item_stale], mapping, VALID, text_rules=None)
    assert resolved[0]["cod_prestatie"] == "OE-2"
    assert "cod_sursa" not in resolved[0], "cod_sursa stale nu a fost curatat"
    assert "regula_fara_autosend" not in resolved[0], "flag stale nu a fost curatat"
    assert has_no_auto_send(resolved, mapping_meta) is False
