"""Teste US-012 / US-012b / US-012c (PRD 5.10): Header logo ROMFAST + titlu centrat.

TDD: testele se scriu INAINTE de implementare (RED), dupa implementare trec (GREEN).

US-012b (decizie user): logo PNG real (/static/romfast_logo.png) in loc de wordmark text.
US-012c (decizie user): logo mutat din header-center in header-left (brand top-left ca pe romfast.ro).
  Decizie env badge: mutat in header-center (sub <h1>, mic, color:var(--muted)) — nu suprapune
  logo-ul si pastreaza centrarea optica a titlului in coloana auto a grilei.

Testeaza:
- test_header_contine_by_romfast: img brand-logo in .header-left (NU in header-center)
- test_titlu_centrat: titlul e in .header-center (grila 3 coloane), controale la dreapta
"""

from __future__ import annotations

import os
import re
import tempfile

import pytest
from starlette.testclient import TestClient


@pytest.fixture()
def client(monkeypatch):
    tmp = tempfile.mkdtemp()
    monkeypatch.setenv("AUTOPASS_DB_PATH", os.path.join(tmp, "branding.db"))
    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()


def _get_header(html: str) -> str:
    """Extrage continutul elementului <header>."""
    m = re.search(r"<header>(.*?)</header>", html, re.DOTALL | re.IGNORECASE)
    assert m, "<header> negasit in HTML"
    return m.group(1)


def _get_style(html: str) -> str:
    """Extrage continutul primului <style>."""
    m = re.search(r"<style>(.*?)</style>", html, re.DOTALL)
    assert m, "<style> negasit in HTML"
    return m.group(1)


def _get_div_content(html: str, cls: str) -> str | None:
    """Extrage continutul primului div cu clasa `cls` (non-nested)."""
    m = re.search(
        r'<div[^>]+class=["\'][^"\']*' + re.escape(cls) + r'[^"\']*["\'][^>]*>(.*?)</div>',
        html,
        re.DOTALL,
    )
    return m.group(1) if m else None


# ── test_header_contine_by_romfast ────────────────────────────────────────────

def test_header_contine_by_romfast(client):
    """Logo-ul ROMFAST (<img class='brand-logo'>) trebuie sa fie in .header-LEFT (US-012c).

    Decizie user: brand top-left ca pe romfast.ro — logo in prima celula a grilei, nu sub titlu.

    Verifica:
    - img cu romfast_logo.png SI class brand-logo exista in interiorul .header-left
    - img cu romfast_logo.png NU exista in interiorul .header-center (a fost mutat)
    - alt non-gol pe img (accesibilitate)
    - .header-center NU mai contine clasele .romfast-rom / .romfast-fast (curatenie)
    """
    resp = client.get("/login")
    assert resp.status_code == 200
    header = _get_header(resp.text)

    # Extrage continutul .header-left si .header-center
    left_content = _get_div_content(header, "header-left")
    center_content = _get_div_content(header, "header-center")
    assert left_content is not None, ".header-left lipseste din <header>"
    assert center_content is not None, ".header-center lipseste din <header>"

    # 1. img cu romfast_logo.png IN .header-left
    img_tags_left = re.findall(r'<img[^>]+>', left_content, re.IGNORECASE)
    logo_in_left = next(
        (t for t in img_tags_left if "romfast_logo.png" in t),
        None,
    )
    assert logo_in_left is not None, (
        "<img> cu 'romfast_logo.png' negasit in .header-left. "
        "US-012c: logo-ul trebuie sa fie in celula STANGA a grilei (brand top-left). "
        f"Continut .header-left: {left_content[:400]}"
    )

    # 2. img cu romfast_logo.png NU mai e in .header-center
    img_tags_center = re.findall(r'<img[^>]+>', center_content, re.IGNORECASE)
    logo_in_center = next(
        (t for t in img_tags_center if "romfast_logo.png" in t),
        None,
    )
    assert logo_in_center is None, (
        "<img> cu 'romfast_logo.png' inca e in .header-center — trebuie mutat in .header-left. "
        f"Continut .header-center: {center_content[:400]}"
    )

    # 3. alt non-gol pe logo (accesibilitate)
    alt_match = re.search(r'alt=["\']([^"\']+)["\']', logo_in_left, re.IGNORECASE)
    assert alt_match and alt_match.group(1).strip(), (
        "Imaginea logo din .header-left lipseste atributul alt (sau e gol). "
        f"Tag gasit: {logo_in_left}"
    )

    # 4. class brand-logo prezent pe img
    assert "brand-logo" in logo_in_left, (
        "class='brand-logo' lipseste de pe <img> logo din .header-left. "
        f"Tag gasit: {logo_in_left}"
    )

    # 5. Spanurile text (wordmark vechi) NU exista in header
    assert "romfast-rom" not in header and "romfast-fast" not in header, (
        "Clasele .romfast-rom / .romfast-fast (wordmark text) inca prezente in header. "
        f"Header snippet: {header[:500]}"
    )


# ── test_titlu_centrat ────────────────────────────────────────────────────────

def test_titlu_centrat(client):
    """Titlul 'Gateway RAR AUTOPASS' e in structura centrata in header (grila 3 coloane).

    Verifica:
    - CSS contine grid-template-columns cu 3 coloane pe header (1fr auto 1fr sau similar)
    - Header contine un element cu clasa 'header-center' (sau similar) care contine h1
    - Controalele (button tema-toggle) sunt la dreapta (in header-right sau margin-left:auto)
    - header-left exista (celula stanga a grilei, contine logo dupa US-012c)
    """
    resp = client.get("/login")
    assert resp.status_code == 200
    html = resp.text
    header = _get_header(html)
    style = _get_style(html)

    # 1. CSS header foloseste grid cu 3 coloane
    header_css = re.search(r"header\s*\{([^}]+)\}", style, re.DOTALL)
    assert header_css, "Regula CSS 'header { ... }' negasita in <style>"
    header_block = header_css.group(1)
    assert "grid" in header_block.lower(), (
        f"header CSS nu foloseste grid (display:grid). Block: {header_block.strip()}"
    )
    assert "grid-template-columns" in style, (
        "grid-template-columns lipseste din <style> (necesar pentru 3 coloane)"
    )

    # 2. Element centrat in header contine h1
    center_div = re.search(
        r'<div[^>]+class=["\'][^"\']*header-center[^"\']*["\'][^>]*>(.*?)</div>',
        header,
        re.DOTALL,
    )
    assert center_div, (
        "Element cu clasa 'header-center' negasit in <header>. "
        "Titlul trebuie sa fie intr-o celula centrata a grilei. "
        f"Header snippet: {header[:600]}"
    )
    center_content = center_div.group(1)
    assert "<h1" in center_content, (
        "h1 nu e in interiorul elementului .header-center. "
        f"Continut .header-center: {center_content[:300]}"
    )

    # 3. Controalele (butonul de tema) sunt in header-right
    right_div = re.search(
        r'<div[^>]+class=["\'][^"\']*header-right[^"\']*["\'][^>]*>(.*?)</div>',
        header,
        re.DOTALL,
    )
    assert right_div, (
        "Element cu clasa 'header-right' negasit in <header>. "
        "Controalele (tema, versiune, meniu) trebuie sa fie la dreapta. "
        f"Header snippet: {header[:600]}"
    )
    right_content = right_div.group(1)
    assert "tema-toggle" in right_content, (
        "Butonul tema-toggle nu e in .header-right. "
        f"Continut .header-right: {right_content[:300]}"
    )

    # 4. header-left exista in grila (contine logo dupa US-012c)
    left_div = re.search(
        r'<div[^>]+class=["\'][^"\']*header-left[^"\']*["\'][^>]*>(.*?)</div>',
        header,
        re.DOTALL,
    )
    assert left_div, (
        "Element cu clasa 'header-left' negasit in <header>. "
        f"Header snippet: {header[:600]}"
    )
