"""
Unit Tests for Alerts Frontend Module

Tests for the refactored alerts frontend components:
- alerts_css.py (CSS styles)
- alerts_html.py (HTML template)
- alerts_js.py (JavaScript)
- alerts.py (AlertsModule class)
"""
import pytest

import sys
sys.path.insert(0, '..')

from frontend.modules.alerts_css import get_alerts_css
from frontend.modules.alerts_html import get_alerts_html
from frontend.modules.alerts_js import get_alerts_js
from frontend.modules.alerts import AlertsModule


class TestAlertsCss:
    """Test CSS styles"""

    def test_get_alerts_css_returns_string(self):
        """Test that get_alerts_css returns a string"""
        result = get_alerts_css()
        assert isinstance(result, str)
        assert len(result) > 0

    def test_css_contains_panel_styles(self):
        """Test that CSS contains panel styles"""
        css = get_alerts_css()
        assert ".panel-alerts" in css
        assert ".alerts-header" in css

    def test_css_contains_inbox_styles(self):
        """Test that CSS contains inbox styles"""
        css = get_alerts_css()
        assert "inbox" in css.lower() or "alert" in css.lower()

    def test_css_contains_layout_classes(self):
        """Test that CSS contains layout classes"""
        css = get_alerts_css()
        assert "display:" in css
        assert "flex" in css or "grid" in css


class TestAlertsHtml:
    """Test HTML template"""

    def test_get_alerts_html_returns_string(self):
        """Test that get_alerts_html returns a string"""
        result = get_alerts_html()
        assert isinstance(result, str)
        assert len(result) > 0

    def test_html_contains_panel_element(self):
        """Test that HTML contains panel element"""
        html = get_alerts_html()
        assert "panel-alerts" in html
        assert "id=" in html

    def test_html_contains_header(self):
        """Test that HTML contains header section"""
        html = get_alerts_html()
        assert "alerts-header" in html

    def test_html_is_valid_structure(self):
        """Test that HTML has valid structure"""
        html = get_alerts_html()
        assert "<div" in html
        assert "</div>" in html


class TestAlertsJs:
    """Test JavaScript"""

    def test_get_alerts_js_returns_string(self):
        """Test that get_alerts_js returns a string"""
        result = get_alerts_js()
        assert isinstance(result, str)
        assert len(result) > 0

    def test_js_contains_functions(self):
        """Test that JS contains function definitions"""
        js = get_alerts_js()
        assert "function" in js or "=>" in js or "const" in js

    def test_js_contains_event_handlers(self):
        """Test that JS contains event handling code"""
        js = get_alerts_js()
        # Should have some event handling
        has_events = any(x in js for x in ['addEventListener', 'onclick', 'click', 'event'])
        assert has_events or len(js) > 100


class TestAlertsModule:
    """Test AlertsModule class"""

    def test_module_has_name(self):
        """Test that module has name attribute"""
        assert hasattr(AlertsModule, 'name')
        assert AlertsModule.name == "alerts"

    def test_module_has_display_name(self):
        """Test that module has display_name attribute"""
        assert hasattr(AlertsModule, 'display_name')
        assert AlertsModule.display_name == "Alerts Agent"

    def test_module_has_icon(self):
        """Test that module has icon attribute"""
        assert hasattr(AlertsModule, 'icon')
        assert AlertsModule.icon == "notification"

    def test_get_css_method(self):
        """Test get_css method"""
        css = AlertsModule.get_css()
        assert isinstance(css, str)
        assert len(css) > 0

    def test_get_html_method(self):
        """Test get_html method"""
        html = AlertsModule.get_html()
        assert isinstance(html, str)
        assert len(html) > 0

    def test_get_js_method(self):
        """Test get_js method"""
        js = AlertsModule.get_js()
        assert isinstance(js, str)
        assert len(js) > 0

    def test_render_method(self):
        """Test render method returns dict with all components"""
        result = AlertsModule.render()
        assert isinstance(result, dict)
        assert "css" in result
        assert "html" in result
        assert "js" in result

    def test_render_components_match_methods(self):
        """Test that render components match individual methods"""
        result = AlertsModule.render()
        assert result["css"] == AlertsModule.get_css()
        assert result["html"] == AlertsModule.get_html()
        assert result["js"] == AlertsModule.get_js()


class TestAlertsModuleIntegration:
    """Integration tests"""

    def test_css_html_js_sizes_reasonable(self):
        """Test that component sizes are reasonable"""
        css = AlertsModule.get_css()
        html = AlertsModule.get_html()
        js = AlertsModule.get_js()

        # Each component should have substantial content
        assert len(css) > 1000, "CSS seems too small"
        assert len(html) > 500, "HTML seems too small"
        assert len(js) > 1000, "JS seems too small"

    def test_module_backwards_compatible(self):
        """Test that module maintains backwards compatibility"""
        # Should be able to import module class
        from frontend.modules.alerts import AlertsModule

        # Should have all expected methods
        assert callable(AlertsModule.get_css)
        assert callable(AlertsModule.get_html)
        assert callable(AlertsModule.get_js)
        assert callable(AlertsModule.render)
