"""Teste US-007 (PRD 5.12): preview compact — scoatere coloana "Verificat?" + VIN compact.

TDD RED: testele sunt scrise inainte de implementare.

Scenarii:
  1. Preview NU contine coloana "Verificat?" (col-verificat / antet).
  2. VIN nu se sparge pe verticala (white-space controlat in templateul randului).
"""

from __future__ import annotations

import io
import os
import re
import tempfile

import pytest
from fastapi.testclient import TestClient


# --------------------------------------------------------------------------- #
# Fixtures                                                                      #
# --------------------------------------------------------------------------- #

@pytest.fixture()
def client(monkeypatch):
    """Client fara autentificare web obligatorie (cont 1 implicit)."""
    tmp = tempfile.mkdtemp()
    monkeypatch.setenv("AUTOPASS_DB_PATH", os.path.join(tmp, "pc.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()


# --------------------------------------------------------------------------- #
# Helpere                                                                       #
# --------------------------------------------------------------------------- #

def _seed_op1(account_id: int = 1) -> None:
    """Semeaza nomenclator + mapare OP-1 → R-FRANE."""
    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 (?, 'OP-1', 'R-FRANE', 1)",
            (account_id,),
        )
        conn.commit()
    finally:
        conn.close()


def _csv_bytes(rows: list[dict], sep: str = ";") -> bytes:
    import csv as _csv
    buf = io.StringIO()
    writer = _csv.DictWriter(buf, fieldnames=list(rows[0].keys()), delimiter=sep)
    writer.writeheader()
    writer.writerows(rows)
    return buf.getvalue().encode("utf-8")


_SAMPLE_ROWS = [
    {
        "VIN": "WVWZZZ1KZAW000123",
        "Nr": "B001TST",
        "Data": "2026-06-10",
        "KM": "123456",
        "Operatie": "OP-1",
    },
]

_MAP_COLS = {
    "VIN": "vin",
    "Nr": "nr_inmatriculare",
    "Data": "data_prestatie",
    "KM": "odometru_final",
    "Operatie": "operatie",
}


def _get_csrf(client: TestClient) -> str:
    r = client.get("/")
    m = re.search(r'name="csrf_token"\s+value="([^"]+)"', r.text) or \
        re.search(r'value="([^"]+)"\s+name="csrf_token"', r.text)
    return m.group(1) if m else ""


def _upload_and_preview(client: TestClient, rows: list[dict] | None = None,
                        format_data: str = "YYYY-MM-DD") -> int:
    """Upload CSV + salveaza mapare → preview. Intoarce import_id."""
    rows = rows or _SAMPLE_ROWS
    csv_data = _csv_bytes(rows)
    csrf = _get_csrf(client)
    r = client.post(
        "/_import/upload",
        files={"file": ("test.csv", io.BytesIO(csv_data), "text/csv")},
        data={"csrf_token": csrf},
    )
    assert r.status_code == 200, r.text
    m = re.search(r"/_import/(\d+)/mapare-coloane", r.text)
    assert m, f"import_id negasit in raspuns: {r.text[:300]}"
    iid = int(m.group(1))
    colnames = list(rows[0].keys())
    canons = [_MAP_COLS[c] for c in colnames]
    csrf2 = _get_csrf(client)
    r2 = client.post(f"/_import/{iid}/mapare-coloane", data={
        "colname": colnames,
        "canon": canons,
        "format_data": format_data,
        "csrf_token": csrf2,
    })
    assert r2.status_code == 200, r2.text
    return iid


# --------------------------------------------------------------------------- #
# Teste                                                                         #
# --------------------------------------------------------------------------- #

def test_preview_fara_coloana_verificat(client):
    """Tabelul de preview NU contine coloana 'Verificat?' (col-verificat).

    US-007 (PRD 5.12): coloana 'Verificat?' este eliminata din preview;
    antetul si celulele scad la 8 coloane (fara col-verificat).

    Verifica:
    - Antetul tabelului NU contine 'Verificat?' ca text
    - NU exista elemente cu clasa 'col-verificat' in HTML-ul de preview
    - NU exista input[name='reviewed_rows'] (checkboxele au fost eliminate)
    """
    _seed_op1()
    iid = _upload_and_preview(client)

    r = client.get(f"/_import/{iid}/preview")
    assert r.status_code == 200, r.text
    html = r.text

    # Coloana "Verificat?" trebuie eliminata
    assert "Verificat?" not in html, \
        "Coloana 'Verificat?' nu trebuie sa apara in antetul tabelului de preview"
    assert "col-verificat" not in html, \
        "Clasa 'col-verificat' nu trebuie sa existe in HTML-ul de preview"

    # Checkboxele reviewed_rows trebuie eliminate
    assert 'name="reviewed_rows"' not in html, \
        "Input[name='reviewed_rows'] (checkbox) nu trebuie sa existe in preview"


def test_preview_vin_nu_se_sparge_pe_verticala(client):
    """VIN-ul din coloana Vehicul nu se mai sparge pe verticala.

    US-007 (PRD 5.12): randuri compacte — VIN cu white-space controlat
    (white-space:nowrap sau min-width pe coloana), fara overflow orizontal.

    Verifica:
    - Divul cu VIN scurt are white-space:nowrap (previne ruperea pe linie noua)
    - SAU coloana col-vehicul are o latime/min-width adecvata
    """
    _seed_op1()
    iid = _upload_and_preview(client)

    r = client.get(f"/_import/{iid}/preview")
    assert r.status_code == 200, r.text
    html = r.text

    # Verifica ca VIN-ul are white-space:nowrap in div-ul de VIN scurt
    # Template-ul _preview_rand.html trebuie sa aiba:
    # <div class="muted" style="...white-space:nowrap...">{{ row.prez.vin_scurt }}</div>
    assert "white-space:nowrap" in html or "white-space: nowrap" in html, \
        "VIN-ul din preview trebuie sa aiba white-space:nowrap pentru a preveni ruperea pe verticala"
