"""Teste US-004 (PRD 5.10): paginare numerotata pe tabelul de trimiteri.

Cazuri:
- pagina_implicita_25: 30 trimiteri → pagina 1 afiseaza max 25
- pagina_2_offset: 30 trimiteri, page=2 → 5 randuri
- total_si_numar_pagini: raspunsul contine totalul + aria-current pe pagina curenta
- paginarea_pastreaza_filtrele: linkurile de paginare includ filtrul status activ
- pagina_peste_total_revine_la_ultima: page=99 cu 30 trimiteri → clamped la pagina 2
- poll_pastreaza_pagina: raspunsul include id='f-page' value='2' (OOB) pentru poll
"""

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) or \
        re.search(r'value="([^"]+)"\s+name="csrf_token"', resp.text)
    assert m
    resp = client.post("/login", data={"email": email, "parola": password, "csrf_token": m.group(1)})
    assert resp.status_code == 303


def _ins_n(acct: int, n: int, status: str = "sent") -> None:
    """Insereaza n submissions pentru contul dat."""
    from app.db import get_connection
    conn = get_connection()
    try:
        for i in range(n):
            conn.execute(
                "INSERT INTO submissions (idempotency_key, account_id, status, payload_json) VALUES (?, ?, ?, ?)",
                (
                    f"k-pg-{os.urandom(6).hex()}", acct, status,
                    json.dumps({
                        "vin": f"WVIN_PG_{i:04d}_DUMMY",
                        "nr_inmatriculare": f"B{i:03d}PG",
                        "data_prestatie": "2026-06-20",
                        "odometru_final": "100",
                        "prestatii": [{"cod_prestatie": "R-X"}],
                    }),
                ),
            )
        conn.commit()
    finally:
        conn.close()


@pytest.fixture()
def client(monkeypatch):
    tmp = tempfile.mkdtemp()
    monkeypatch.setenv("AUTOPASS_DB_PATH", os.path.join(tmp, "paginare.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_pagina_implicita_25(client):
    """Cu 30 trimiteri, pagina 1 (implicita) returneaza exact 25 randuri."""
    acct = _create_account_user("pg1@test.com")
    _ins_n(acct, 30)
    _login(client, "pg1@test.com")

    resp = client.get("/_fragments/submissions")
    assert resp.status_code == 200
    body = resp.text
    row_count = body.count('id="trimitere-row-')
    assert row_count == 25, (
        f"Pagina 1 cu 30 trimiteri trebuie sa arate exact 25 randuri, nu {row_count}"
    )


def test_pagina_2_offset(client):
    """Cu 30 trimiteri, page=2 returneaza restul de 5 randuri (offset 25)."""
    acct = _create_account_user("pg2@test.com")
    _ins_n(acct, 30)
    _login(client, "pg2@test.com")

    resp = client.get("/_fragments/submissions?page=2")
    assert resp.status_code == 200
    body = resp.text
    row_count = body.count('id="trimitere-row-')
    assert row_count == 5, (
        f"Pagina 2 cu 30 total trebuie sa arate 5 randuri, nu {row_count}"
    )


def test_total_si_numar_pagini(client):
    """Raspunsul contine totalul (30) si marcheaza pagina curenta cu aria-current='page'."""
    acct = _create_account_user("pg3@test.com")
    _ins_n(acct, 30)
    _login(client, "pg3@test.com")

    resp = client.get("/_fragments/submissions")
    assert resp.status_code == 200
    body = resp.text
    # Totalul trebuie afisat undeva (ex. "30 din 30" sau "afiseaza 1-25 din 30")
    assert "30" in body, "Totalul 30 trebuie sa apara in raspuns"
    # Pagina curenta e marcata semantic
    assert 'aria-current="page"' in body, (
        "Pagina curenta trebuie marcata cu aria-current='page'"
    )


def test_paginarea_pastreaza_filtrele(client):
    """Linkurile de paginare pastreaza filtrul status activ in URL."""
    acct = _create_account_user("pg4@test.com")
    _ins_n(acct, 30, status="needs_data")
    _login(client, "pg4@test.com")

    resp = client.get("/_fragments/submissions?status=needs_data&page=1")
    assert resp.status_code == 200
    body = resp.text
    # Pager-ul exista si linkurile contin status=needs_data
    assert "status=needs_data" in body, (
        "Linkurile de paginare trebuie sa pastreze filtrul status=needs_data"
    )


def test_pagina_peste_total_revine_la_ultima(client):
    """page=99 cu 30 trimiteri se clampeaza la ultima pagina (page 2 → 5 randuri)."""
    acct = _create_account_user("pg5@test.com")
    _ins_n(acct, 30)
    _login(client, "pg5@test.com")

    resp = client.get("/_fragments/submissions?page=99")
    assert resp.status_code == 200
    body = resp.text
    row_count = body.count('id="trimitere-row-')
    assert row_count == 5, (
        f"page=99 cu 30 trimiteri trebuie clamped la ultima pagina (5 randuri), nu {row_count}"
    )


def test_poll_pastreaza_pagina(client):
    """Raspunsul de la page=2 include id='f-page' value='2' (OOB swap) pentru poll.

    Mecanismul: _submissions.html include un element cu id='f-page' si hx-swap-oob='true'
    care actualizeaza inputul ascuns din #filtre-trimiteri. Poll-ul de 15s (hx-include=
    '#filtre-trimiteri') include astfel pagina curenta la urmatoarea iteratie (L2 PRD).
    """
    acct = _create_account_user("pg6@test.com")
    _ins_n(acct, 30)
    _login(client, "pg6@test.com")

    resp = client.get("/_fragments/submissions?page=2")
    assert resp.status_code == 200
    body = resp.text
    # Elementul OOB trebuie sa fie in raspuns cu valoarea corecta
    assert 'id="f-page"' in body, (
        "Raspunsul trebuie sa includa id='f-page' (OOB swap) pentru ca poll-ul sa pastreze pagina"
    )
    assert 'value="2"' in body, (
        "Elementul f-page trebuie sa aiba value='2' cand page=2"
    )
