"""Teste US-002 (PRD 5.11) — scoate bifa auto_send din UI.

Verifica ca bifa "In coada automat" (macro autosend_toggle) nu mai apare in:
  - tab-ul Mapari (/_fragments/mapari)
  - panoul de mapare din preview (pas 3 import)
  - panoul de detaliu trimitere (_trimitere_detaliu.html)
"""

from __future__ import annotations

import csv
import io
import json
import os
import re
import tempfile

import pytest
from fastapi.testclient import TestClient


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


@pytest.fixture()
def conn(client):
    from app.db import get_connection
    c = get_connection()
    yield c
    c.close()


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


def _seed_nomenclator(conn, cod="OE-1"):
    conn.execute(
        "INSERT OR IGNORE INTO nomenclator_rar (cod_prestatie, nume_prestatie) VALUES (?, ?)",
        (cod, f"Operatie {cod}"),
    )
    conn.execute(
        "INSERT OR IGNORE INTO operations_mapping (account_id, cod_op_service, cod_prestatie, auto_send) "
        "VALUES (1, 'OP-EXISTENT', ?, 1)",
        (cod,),
    )
    conn.commit()


def test_mapari_fara_toggle_autosend(client, conn):
    """GET /_fragments/mapari nu contine checkbox auto_send sau coloana 'In coada'.

    Dupa US-002: macro autosend_toggle golit; coloana In coada scoasa din toate tabelele.
    """
    _seed_nomenclator(conn)
    r = client.get("/_fragments/mapari")
    assert r.status_code == 200
    body = r.text
    assert 'name="auto_send"' not in body, (
        "tab-ul Mapari nu trebuie sa contina input auto_send dupa US-002"
    )
    assert "autosend-toggle" not in body, (
        "tab-ul Mapari nu trebuie sa contina clasa autosend-toggle dupa US-002"
    )
    assert "In coada" not in body, (
        "tab-ul Mapari nu trebuie sa contina coloana 'In coada' dupa US-002"
    )


def test_preview_panou_mapare_fara_autosend(client):
    """Panoul de mapare din preview (pas 3) nu contine checkbox auto_send.

    Dupa US-002: macro autosend_toggle golit din _preview_import.html.
    """
    # Upload CSV cu operatie nemapata -> map coloane -> ajunge la preview
    rows = [
        {"VIN": "WVWZZZ1KZAW000123", "Nr inmatriculare": "B001TST",
         "Data prestatie": "15.06.2026", "Odometru final": "123456",
         "Operatie": "OP-NEMAPATA-US002"},
    ]
    data = _csv_bytes(rows)
    r = client.post("/_import/upload", files={"file": ("test.csv", data, "text/csv")})
    assert r.status_code == 200
    m = re.search(r"/_import/(\d+)/mapare-coloane", r.text)
    assert m, f"Nu am gasit import_id in raspuns: {r.text[:300]}"
    import_id = int(m.group(1))

    # Salveaza maparea coloanelor -> genereaza preview
    r = client.post(f"/_import/{import_id}/mapare-coloane", data={
        "colname": ["VIN", "Nr inmatriculare", "Data prestatie", "Odometru final", "Operatie"],
        "canon": ["vin", "nr_inmatriculare", "data_prestatie", "odometru_final", "operatie"],
        "format_data": "DD.MM.YYYY",
    })
    assert r.status_code == 200

    # Obtine preview-ul HTML (ar trebui sa aiba panoul de mapare cu OP-NEMAPATA-US002)
    r2 = client.get(f"/_import/{import_id}/preview")
    assert r2.status_code == 200
    body = r2.text
    # Panoul de mapare apare (operatia nemapata e prezenta)
    assert "OP-NEMAPATA-US002" in body, "preview-ul trebuie sa contina operatia nemapata"
    # Fara bifa auto_send
    assert 'name="auto_send"' not in body, (
        "preview-ul nu trebuie sa contina input auto_send dupa US-002"
    )
    assert "autosend-toggle" not in body, (
        "preview-ul nu trebuie sa contina clasa autosend-toggle dupa US-002"
    )
    assert "In coada automat" not in body, (
        "preview-ul nu trebuie sa contina 'In coada automat' dupa US-002"
    )


def test_detaliu_mapare_inline_fara_autosend(client, conn):
    """GET /_fragments/trimitere/{id} cu needs_mapping nu contine checkbox auto_send.

    Dupa US-002: macro autosend_toggle golit din _trimitere_detaliu.html.
    """
    conn.execute(
        "INSERT OR IGNORE INTO nomenclator_rar (cod_prestatie, nume_prestatie) VALUES ('OE-1', 'Test')"
    )
    payload = {
        "vin": "WVWZZZ1KZAW000123", "nr_inmatriculare": "B1",
        "data_prestatie": "2026-06-15", "odometru_final": "123456",
        "prestatii": [{"cod_op_service": "ITP-US002", "denumire": "Inspectie",
                       "cod_prestatie": None}],
    }
    sid = conn.execute(
        "INSERT INTO submissions (idempotency_key, account_id, status, payload_json, rar_error) "
        "VALUES (?, ?, ?, ?, ?)",
        ("k-us002-det", 1, "needs_mapping", json.dumps(payload),
         json.dumps({"unmapped": [{"cod_op_service": "ITP-US002"}]})),
    ).lastrowid
    conn.commit()

    r = client.get(f"/_fragments/trimitere/{sid}")
    assert r.status_code == 200
    body = r.text
    # Operatia nemapata e afisata
    assert "ITP-US002" in body, "detaliul trebuie sa contina operatia nemapata"
    assert 'name="auto_send"' not in body, (
        "detaliul nu trebuie sa contina input auto_send dupa US-002"
    )
    assert "autosend-toggle" not in body, (
        "detaliul nu trebuie sa contina clasa autosend-toggle dupa US-002"
    )
