"""Teste US-009 (PRD 5.8) — preview pre-salvare regula text.

POST /mapari/reguli-text/preview primeste `pattern`, normalizeaza cu
normalize_for_match, numara operatiile DISTINCTE nemapate ale contului
(needs_mapping, reuse pending_unmapped) al caror text contine pattern-ul si
intoarce pana la 3 exemple. NU salveaza nimic (zero scriere DB). Pattern gol ->
fragment gol (nu numara „tot"). Scoped pe contul sesiunii (require_login + CSRF).
"""

from __future__ import annotations

import json
import os
import re
import tempfile

import pytest
from starlette.testclient import TestClient


def _create_account_user(email: str, name: str = "Service", password: str = "parolasecreta10"):
    from app.accounts import create_account
    from app.users import create_user
    from app.db import get_connection

    conn = get_connection()
    try:
        acct_id = create_account(conn, name, active=True)
        create_user(conn, acct_id, email, password)
        return acct_id
    finally:
        conn.close()


def _login(client, email: str, password: str = "parolasecreta10") -> None:
    resp = client.get("/login")
    m = re.search(r'name="csrf_token"\s+value="([^"]+)"', resp.text)
    assert m
    resp = client.post("/login", data={"email": email, "parola": password, "csrf_token": m.group(1)})
    assert resp.status_code == 303


def _csrf(client) -> str:
    resp = client.get("/?tab=mapari")
    m = re.search(r'name="csrf_token"\s+value="([^"]+)"', resp.text)
    assert m, "csrf_token negasit"
    return m.group(1)


def _seed_needs_mapping(acct: int, *, op: str, denumire: str | None = None) -> int:
    """Submission needs_mapping pe canal API (batch_id NULL) cu o operatie nemapata."""
    from app.db import get_connection
    conn = get_connection()
    try:
        cur = conn.execute(
            "INSERT INTO submissions (idempotency_key, account_id, status, payload_json, rar_error) "
            "VALUES (?, ?, 'needs_mapping', ?, ?)",
            (
                f"k-{os.urandom(6).hex()}",
                acct,
                json.dumps({
                    "vin": "WVWZZZ1JZXW000111",
                    "nr_inmatriculare": "B11AAA",
                    "data_prestatie": "2026-06-18",
                    "odometru_final": "12345",
                    "prestatii": [{"cod_prestatie": None, "cod_op_service": op, "denumire": denumire or op}],
                }),
                json.dumps({"unmapped": [{"cod_op_service": op}]}),
            ),
        )
        conn.commit()
        return int(cur.lastrowid)
    finally:
        conn.close()


def _count_submissions() -> int:
    from app.db import get_connection
    conn = get_connection()
    try:
        return int(conn.execute("SELECT COUNT(*) AS c FROM submissions").fetchone()["c"])
    finally:
        conn.close()


def _count_text_rules() -> int:
    from app.db import get_connection
    conn = get_connection()
    try:
        return int(conn.execute("SELECT COUNT(*) AS c FROM operation_text_rules").fetchone()["c"])
    finally:
        conn.close()


@pytest.fixture()
def client(monkeypatch):
    tmp = tempfile.mkdtemp()
    monkeypatch.setenv("AUTOPASS_DB_PATH", os.path.join(tmp, "preview_regula.db"))
    monkeypatch.setenv("AUTOPASS_WEB_AUTH_REQUIRED", "true")
    from app.config import get_settings
    get_settings.cache_clear()
    from app.web import ratelimit
    ratelimit._hits.clear()
    from app.main import app
    with TestClient(app, follow_redirects=False) as c:
        yield c
    ratelimit._hits.clear()
    get_settings.cache_clear()


def test_preview_numara_potriviri(client):
    """Numara operatiile DISTINCTE nemapate al caror text contine pattern-ul."""
    acct = _create_account_user("p1@test.com")
    _seed_needs_mapping(acct, op="VERIFICARE FARURI", denumire="Verificare faruri")
    _seed_needs_mapping(acct, op="VERIFICARE FRANE", denumire="Verificare frane")
    _seed_needs_mapping(acct, op="SCHIMB ULEI", denumire="Schimb ulei")

    _login(client, "p1@test.com")
    csrf = _csrf(client)
    before = _count_submissions()
    resp = client.post("/mapari/reguli-text/preview", data={
        "pattern": "verificare", "csrf_token": csrf,
    })
    assert resp.status_code == 200
    # Cele doua operatii „verificare", nu si „schimb ulei".
    assert "2" in resp.text
    assert "potriveste" in resp.text.lower()
    # NU salveaza nimic: nici submission, nici regula.
    assert _count_submissions() == before
    assert _count_text_rules() == 0


def test_preview_intoarce_exemple(client):
    """Fragmentul include exemple (denumirea/operatia care potrivesc)."""
    acct = _create_account_user("p2@test.com")
    _seed_needs_mapping(acct, op="VERIFICARE FARURI", denumire="Verificare faruri")
    _seed_needs_mapping(acct, op="VERIFICARE FRANE", denumire="Verificare frane")

    _login(client, "p2@test.com")
    csrf = _csrf(client)
    resp = client.post("/mapari/reguli-text/preview", data={
        "pattern": "verificare", "csrf_token": csrf,
    })
    assert resp.status_code == 200
    text = resp.text.lower()
    assert "faruri" in text or "frane" in text


def test_preview_pattern_gol(client):
    """Pattern gol -> fragment gol; nu numara „tot"."""
    acct = _create_account_user("p3@test.com")
    _seed_needs_mapping(acct, op="VERIFICARE FARURI", denumire="Verificare faruri")

    _login(client, "p3@test.com")
    csrf = _csrf(client)
    resp = client.post("/mapari/reguli-text/preview", data={
        "pattern": "   ", "csrf_token": csrf,
    })
    assert resp.status_code == 200
    # Nu raporteaza nicio potrivire numerica pentru pattern gol.
    assert "potriveste" not in resp.text.lower()
    assert resp.text.strip() == "" or "1" not in resp.text


def test_preview_scoped_pe_cont(client):
    """Numara DOAR operatiile contului sesiunii, nu ale altui cont."""
    acct_a = _create_account_user("a@test.com", name="Cont A")
    acct_b = _create_account_user("b@test.com", name="Cont B")
    # Contul A are 2 potriviri, contul B niciuna.
    _seed_needs_mapping(acct_a, op="VERIFICARE FARURI", denumire="Verificare faruri")
    _seed_needs_mapping(acct_a, op="VERIFICARE FRANE", denumire="Verificare frane")

    _login(client, "b@test.com")
    csrf = _csrf(client)
    resp = client.post("/mapari/reguli-text/preview", data={
        "pattern": "verificare", "csrf_token": csrf,
    })
    assert resp.status_code == 200
    # Contul B nu are nicio operatie nemapata -> nicio potrivire.
    assert "Nicio potrivire" in resp.text
