"""Teste US-002 (PRD 3.6): buton "Editeaza" pe rand in tabelul de preview.

Mod editare pe rand (form propriu, NU #confirm-form). Swap pe rand + OOB contoare,
NU pe #import-section (D-3.1). La eroare de validare randul ramane in editare cu
valorile pastrate (D-2.1/D-2.2). Enter intr-un camp salveaza randul, nu confirma (D-3.3).
"""

from __future__ import annotations

import io
import os
import pathlib
import re
import tempfile

import pytest
from fastapi.testclient import TestClient

_FIXTURES = pathlib.Path(__file__).parent / "fixtures"


@pytest.fixture()
def client(monkeypatch):
    tmp = tempfile.mkdtemp()
    monkeypatch.setenv("AUTOPASS_DB_PATH", os.path.join(tmp, "edit_ui.db"))
    monkeypatch.setenv("AUTOPASS_WEB_AUTH_REQUIRED", "false")
    from app.config import get_settings
    get_settings.cache_clear()
    from app.crypto import reset_cache
    reset_cache()
    from app.main import app
    with TestClient(app) as c:
        yield c
    get_settings.cache_clear()
    reset_cache()


def _seed_op1() -> None:
    from app.db import get_connection
    conn = get_connection()
    try:
        conn.execute(
            "INSERT OR REPLACE INTO nomenclator_rar (cod_prestatie, nume_prestatie) "
            "VALUES ('R-FRANE','Reparatie frane')"
        )
        conn.execute(
            "INSERT OR IGNORE INTO operations_mapping (account_id, cod_op_service, cod_prestatie, auto_send) "
            "VALUES (1, 'OP-1', 'R-FRANE', 1)"
        )
        conn.commit()
    finally:
        conn.close()


def _upload_and_preview(client: TestClient) -> int:
    """Upload fixture necanonic + salveaza maparea -> preview. Intoarce import_id."""
    data = (_FIXTURES / "import_antet_necanonic.csv").read_bytes()
    r = client.post(
        "/_import/upload",
        files={"file": ("import_antet_necanonic.csv", io.BytesIO(data), "text/csv")},
    )
    assert r.status_code == 200, r.text
    m = re.search(r"/_import/(\d+)/mapare-coloane", r.text)
    assert m, "import_id negasit in formularul de mapare"
    iid = int(m.group(1))
    r = client.post(f"/_import/{iid}/mapare-coloane", data={
        "colname": ["Serie sasiu", "Nr", "Data", "KM", "Operatie"],
        "canon": ["vin", "nr_inmatriculare", "data_prestatie", "odometru_final", "operatie"],
        "format_data": "YYYY-MM-DD",
    })
    assert r.status_code == 200, r.text
    return iid


def test_preview_are_buton_editeaza_pe_rand(client):
    """Tabelul de preview are buton 'Editeaza' pe rand (coloana de actiuni)."""
    _seed_op1()
    iid = _upload_and_preview(client)
    r = client.get(f"/_import/{iid}/preview")
    assert r.status_code == 200
    assert "Editeaza" in r.text
    assert f"/_import/{iid}/rand/0/editare" in r.text


def test_editeaza_intra_in_mod_editare_form_propriu(client):
    """GET editare: randul devine un FORM separat care posteaza la .../editeaza, NU #confirm-form."""
    _seed_op1()
    iid = _upload_and_preview(client)
    r = client.get(f"/_import/{iid}/rand/0/editare")
    assert r.status_code == 200
    html = r.text
    assert f'hx-post="/_import/{iid}/rand/0/editeaza"' in html
    # Inputurile de editare NU stau in #confirm-form (form propriu).
    assert 'id="confirm-form"' not in html
    assert 'name="data_prestatie"' in html and 'name="vin"' in html


def test_salveaza_cere_reincarcare_si_toast(client):
    """POST editeaza: raspuns minimal + HX-Trigger(reincarcaPreview + randSalvat).

    Contractul nou (dogfood 5.13): nu mai facem OOB swap pe <tr> (fragil in htmx 1.9 ->
    randul ramanea cu starea veche). Raspunsul cere reincarcarea preview-ului si emite
    detaliile randului salvat pentru toast/evidentiere."""
    import json as _json
    _seed_op1()
    iid = _upload_and_preview(client)
    r = client.post(f"/_import/{iid}/rand/0/editeaza", data={"data_prestatie": "2026-06-10"})
    assert r.status_code == 200
    trig = _json.loads(r.headers.get("HX-Trigger", "{}"))
    assert trig.get("reincarcaPreview") is True
    assert trig.get("randSalvat", {}).get("nr") == 1
    # Raspunsul e doar un stub; randul real vine din reload-ul preview-ului.
    assert 'id="preview-row-0"' not in r.text
    assert 'id="import-section"' not in r.text


def test_enter_in_camp_editare_nu_declanseaza_confirm(client):
    """Inputurile de editare sunt in form propriu (post editeaza); Enter salveaza randul,
    nu declanseaza confirmarea ireversibila. Niciun input de editare nu e legat de #confirm-form."""
    _seed_op1()
    iid = _upload_and_preview(client)
    r = client.get(f"/_import/{iid}/rand/0/editare")
    assert r.status_code == 200
    html = r.text
    # Formul de editare posteaza la editeaza (Enter -> save), nu la /confirma.
    assert f'hx-post="/_import/{iid}/rand/0/editeaza"' in html
    assert "/confirma" not in html
    # Inputurile NU sunt asociate explicit la #confirm-form.
    assert 'form="confirm-form"' not in html


def test_eroare_validare_pastreaza_valorile_introduse(client):
    """Data invalida -> randul ramane in editare, valoarea introdusa pastrata, mesaj pe camp."""
    _seed_op1()
    iid = _upload_and_preview(client)
    r = client.post(f"/_import/{iid}/rand/0/editeaza", data={"data_prestatie": "data-gresita"})
    assert r.status_code == 200
    html = r.text
    # Inca in editare (form propriu prezent) cu valoarea pastrata.
    assert f'hx-post="/_import/{iid}/rand/0/editeaza"' in html
    assert "data-gresita" in html
    # Mesaj de validare pentru data.
    assert "data" in html.lower() and ("invalid" in html.lower() or "YYYY-MM-DD" in html)
