"""Teste T4 — payload builder (app.payload.build_rar_payload).

Snapshot fata de exemplul oficial de request din docs/api-rar-contract.md +
invariantii din contract (status FINALIZATA, fara tipPrestatie, odometru string,
idPrezentare null, omitere obs/b64Image, normalizare).
"""

from __future__ import annotations

from app.models import PrezentareIn
from app.payload import build_rar_payload


def _internal(**over) -> dict:
    data = {
        "vin": "WVWZZZ1KZAW000123",
        "nr_inmatriculare": "B999TST",
        "data_prestatie": "2026-06-15",
        "odometru_final": "123456",
        "prestatii": [{"cod_prestatie": "OE-1"}],
    }
    data.update(over)
    return PrezentareIn(**data).model_dump()


def test_snapshot_exemplul_oficial_din_contract():
    """Payload == exemplul oficial postPrezentare (request) din contract."""
    internal = {
        "vin": "XXXXXXXXXXXXXXXXX",
        "nr_inmatriculare": "B999GEN",
        "data_prestatie": "2024-12-01",  # exemplul are 2024-07-25, dar acela e < min; folosim o data valida, restul identic
        "odometru_final": "9999999",
        "odometru_initial": None,
        "prestatii": [{"cod_prestatie": "OE-1"}, {"cod_prestatie": "OE-2"}],
        "sistem_reparat": "null",
        "obs": "TEST",
        "b64_image": "UklGRg==",
    }
    expected = {
        "vin": "XXXXXXXXXXXXXXXXX",
        "nrInmatriculare": "B999GEN",
        "dataPrestatie": "2024-12-01",
        "odometruFinal": "9999999",
        "odometruInitial": None,
        "prestatii": [
            {"codPrestatie": "OE-1", "idPrezentare": None},
            {"codPrestatie": "OE-2", "idPrezentare": None},
        ],
        "sistemReparat": "null",
        "status": "FINALIZATA",
        "obs": "TEST",
        "b64Image": "UklGRg==",
    }
    assert build_rar_payload(PrezentareIn(**internal).model_dump()) == expected


def test_status_mereu_finalizata():
    assert build_rar_payload(_internal())["status"] == "FINALIZATA"


def test_nu_trimite_tipprestatie():
    assert "tipPrestatie" not in build_rar_payload(_internal())


def test_odometru_final_este_string():
    p = build_rar_payload(_internal(odometru_final="55000"))
    assert p["odometruFinal"] == "55000"
    assert isinstance(p["odometruFinal"], str)


def test_omite_obs_si_b64_cand_lipsesc():
    p = build_rar_payload(_internal())
    assert "obs" not in p
    assert "b64Image" not in p


def test_odometru_initial_gol_devine_null():
    assert build_rar_payload(_internal(odometru_initial=""))["odometruInitial"] is None
    assert build_rar_payload(_internal())["odometruInitial"] is None


def test_odometru_initial_prezent_string():
    p = build_rar_payload(_internal(prestatii=[{"cod_prestatie": "R-ODO"}], odometru_initial="100000"))
    assert p["odometruInitial"] == "100000"


def test_prestatii_au_idprezentare_null():
    p = build_rar_payload(_internal(prestatii=[{"cod_prestatie": "OE-1"}, {"cod_prestatie": "OE-3"}]))
    assert p["prestatii"] == [
        {"codPrestatie": "OE-1", "idPrezentare": None},
        {"codPrestatie": "OE-3", "idPrezentare": None},
    ]


def test_normalizare_vin_nrinm_in_payload():
    p = build_rar_payload(_internal(vin="  wvwzzz1kzaw000123 ", nr_inmatriculare=" b999tst "))
    assert p["vin"] == "WVWZZZ1KZAW000123"
    assert p["nrInmatriculare"] == "B999TST"


def test_sistem_reparat_default_null():
    assert build_rar_payload(_internal())["sistemReparat"] == "null"
