"""
Tests for Phase 90: Server-Side UI Component Library.
"""

import pytest


# No storage to isolate -- pure rendering functions.


# ── 1. Theme ───────────────────────────────────────────────────────────────


class TestTheme:
    def test_get_theme_returns_dict(self):
        from brynq.ui_components import get_theme
        theme = get_theme()
        assert isinstance(theme, dict)
        assert "bg_primary" in theme
        assert "accent" in theme

    def test_theme_has_all_keys(self):
        from brynq.ui_components import get_theme
        theme = get_theme()
        expected = {"bg_primary", "bg_secondary", "bg_card", "text_primary",
                    "text_secondary", "accent", "accent_hover", "success",
                    "warning", "error", "info", "border", "font_family", "radius"}
        assert expected.issubset(set(theme.keys()))

    def test_theme_returns_copy(self):
        from brynq.ui_components import get_theme
        t1 = get_theme()
        t2 = get_theme()
        assert t1 == t2
        t1["accent"] = "changed"
        assert get_theme()["accent"] != "changed"


# ── 2. Card ────────────────────────────────────────────────────────────────


class TestCard:
    def test_basic_card(self):
        from brynq.ui_components import render_card
        html = render_card("Title", "Content")
        assert "Title" in html
        assert "Content" in html
        assert '<div class="card"' in html

    def test_card_with_footer(self):
        from brynq.ui_components import render_card
        html = render_card("T", "C", footer="Footer text")
        assert "Footer text" in html
        assert "card-footer" in html

    def test_card_with_accent(self):
        from brynq.ui_components import render_card
        html = render_card("T", "C", accent=True)
        assert "border-left:3px solid" in html

    def test_card_without_accent(self):
        from brynq.ui_components import render_card
        html = render_card("T", "C", accent=False)
        assert "border-left:3px solid" not in html

    def test_card_escapes_title(self):
        from brynq.ui_components import render_card
        html = render_card("<script>alert(1)</script>", "safe")
        assert "<script>" not in html
        assert "&lt;script&gt;" in html


# ── 3. Table ───────────────────────────────────────────────────────────────


class TestTable:
    def test_basic_table(self):
        from brynq.ui_components import render_table
        html = render_table(["Name", "Value"], [["foo", "bar"]])
        assert "<table" in html
        assert "Name" in html
        assert "foo" in html

    def test_sortable_table(self):
        from brynq.ui_components import render_table
        html = render_table(["A"], [["1"]], sortable=True)
        assert 'class="sortable"' in html

    def test_non_sortable_table(self):
        from brynq.ui_components import render_table
        html = render_table(["A"], [["1"]], sortable=False)
        assert 'class="sortable"' not in html

    def test_multiple_rows(self):
        from brynq.ui_components import render_table
        rows = [["a", "1"], ["b", "2"], ["c", "3"]]
        html = render_table(["Letter", "Number"], rows)
        assert html.count("<tr>") == 4  # 1 header + 3 data rows

    def test_table_escapes_cells(self):
        from brynq.ui_components import render_table
        html = render_table(["H"], [["<b>bold</b>"]])
        assert "<b>" not in html
        assert "&lt;b&gt;" in html


# ── 4. Stats Bar ──────────────────────────────────────────────────────────


class TestStatsBar:
    def test_basic_stats(self):
        from brynq.ui_components import render_stats_bar
        stats = [{"label": "Tasks", "value": 42}, {"label": "Score", "value": 95}]
        html = render_stats_bar(stats)
        assert "stats-bar" in html
        assert "42" in html
        assert "Tasks" in html

    def test_empty_stats(self):
        from brynq.ui_components import render_stats_bar
        html = render_stats_bar([])
        assert "stats-bar" in html


# ── 5. Navigation ─────────────────────────────────────────────────────────


class TestNav:
    def test_basic_nav(self):
        from brynq.ui_components import render_nav
        items = [{"label": "Home", "href": "/"}, {"label": "About", "href": "/about"}]
        html = render_nav(items)
        assert "<nav" in html
        assert "Home" in html
        assert 'href="/"' in html

    def test_active_item(self):
        from brynq.ui_components import render_nav, get_theme
        items = [{"label": "Home", "href": "/"}]
        html = render_nav(items, active="Home")
        accent = get_theme()["accent"]
        assert accent in html

    def test_logged_in(self):
        from brynq.ui_components import render_nav
        html = render_nav([], logged_in=True)
        assert "Logged In" in html

    def test_guest(self):
        from brynq.ui_components import render_nav
        html = render_nav([], logged_in=False)
        assert "Guest" in html


# ── 6. Alert ──────────────────────────────────────────────────────────────


class TestAlert:
    def test_info_alert(self):
        from brynq.ui_components import render_alert
        html = render_alert("Hello", severity="info")
        assert "Hello" in html
        assert "alert-info" in html

    def test_error_alert(self):
        from brynq.ui_components import render_alert
        html = render_alert("Oops", severity="error")
        assert "alert-error" in html

    def test_warning_alert(self):
        from brynq.ui_components import render_alert
        html = render_alert("Watch out", severity="warning")
        assert "alert-warning" in html

    def test_success_alert(self):
        from brynq.ui_components import render_alert
        html = render_alert("Done", severity="success")
        assert "alert-success" in html

    def test_alert_escapes_message(self):
        from brynq.ui_components import render_alert
        html = render_alert("<img onerror=alert(1)>")
        assert "<img" not in html
        assert "&lt;img" in html


# ── 7. Form ───────────────────────────────────────────────────────────────


class TestForm:
    def test_basic_form(self):
        from brynq.ui_components import render_form
        fields = [{"name": "email", "type": "email", "label": "Email"}]
        html = render_form(fields, action="/submit")
        assert "<form" in html
        assert 'action="/submit"' in html
        assert 'name="email"' in html

    def test_form_method(self):
        from brynq.ui_components import render_form
        html = render_form([], action="/a", method="GET")
        assert 'method="GET"' in html

    def test_form_submit_text(self):
        from brynq.ui_components import render_form
        html = render_form([], action="/a", submit_text="Go!")
        assert "Go!" in html

    def test_required_field(self):
        from brynq.ui_components import render_form
        fields = [{"name": "n", "type": "text", "label": "N", "required": True}]
        html = render_form(fields, action="/a")
        assert "required" in html


# ── 8. Agent Card ─────────────────────────────────────────────────────────


class TestAgentCard:
    def test_agent_card(self):
        from brynq.ui_components import render_agent_card
        data = {"name": "Bot-7", "status": "online", "score": 85,
                "skills": ["python", "testing"]}
        html = render_agent_card(data)
        assert "Bot-7" in html
        assert "online" in html
        assert "python" in html

    def test_offline_agent(self):
        from brynq.ui_components import render_agent_card
        data = {"name": "Bot-X", "status": "offline", "score": 0, "skills": []}
        html = render_agent_card(data)
        assert "offline" in html
        assert "None" in html


# ── 9. Task Card ──────────────────────────────────────────────────────────


class TestTaskCard:
    def test_task_card(self):
        from brynq.ui_components import render_task_card
        data = {"title": "Fix bug", "status": "open", "assignee": "Agent-1",
                "priority": "high"}
        html = render_task_card(data)
        assert "Fix bug" in html
        assert "open" in html
        assert "Agent-1" in html

    def test_completed_task(self):
        from brynq.ui_components import render_task_card
        data = {"title": "Done task", "status": "completed", "assignee": "A",
                "priority": "low"}
        html = render_task_card(data)
        assert "completed" in html
