"""Tests para generate_html_report."""

from generate_html_report import generate_html_report


def test_reporte_con_una_tabla():
    sections = [
        {
            "heading": "Datos",
            "type": "table",
            "data": [{"nombre": "Ana", "score": 99}, {"nombre": "Bob", "score": 87}],
        }
    ]
    html = generate_html_report("Reporte", sections)
    assert "<!DOCTYPE html>" in html
    assert "<title>Reporte</title>" in html
    assert "<th>nombre</th>" in html
    assert "<td>Ana</td>" in html
    assert "zebra" in html  # segunda fila tiene class zebra


def test_reporte_con_multiples_secciones_mixtas():
    sections = [
        {"heading": "Texto", "type": "text", "data": "Hola mundo"},
        {"heading": "Lista", "type": "list", "data": ["uno", "dos", "tres"]},
        {"heading": "KPIs", "type": "kpi", "data": [{"label": "Revenue", "value": "1M", "delta": None}]},
    ]
    html = generate_html_report("Multi", sections)
    assert "<p>Hola mundo</p>" in html
    assert "<li>uno</li>" in html
    assert "Revenue" in html
    assert "1M" in html


def test_kpi_con_deltas_positivos_y_negativos():
    sections = [
        {
            "heading": "Metricas",
            "type": "kpi",
            "data": [
                {"label": "Ganancia", "value": "5K", "delta": "+12%"},
                {"label": "Perdida", "value": "2K", "delta": "-5%"},
                {"label": "Estable", "value": "1K", "delta": "0%"},
            ],
        }
    ]
    html = generate_html_report("KPIs", sections)
    assert 'class="delta-pos"' in html
    assert 'class="delta-neg"' in html
    assert 'class="delta-neutral"' in html
    assert "+12%" in html
    assert "-5%" in html


def test_caracteres_especiales_html_escapados_en_data():
    sections = [
        {
            "heading": "Codigo",
            "type": "table",
            "data": [{"expr": "<script>alert('xss')</script>"}],
        }
    ]
    html = generate_html_report("Seguro", sections)
    assert "<script>" not in html
    assert "&lt;script&gt;" in html


def test_titulo_con_caracteres_especiales():
    html = generate_html_report("Reporte & Analisis <2024>", [])
    assert "Reporte &amp; Analisis &lt;2024&gt;" in html
    assert "<title>Reporte &amp; Analisis &lt;2024&gt;</title>" in html
