"""
Unit Tests for Meetings Frontend Module

Tests for the refactored meetings frontend components:
- meetings_styles.py (CSS and Icons)
- meetings_templates.py (Sidebar and Base-Page Templates)
- meetings.py (Route handlers)
"""
import pytest
from unittest.mock import patch, MagicMock
from fastapi.testclient import TestClient
from fastapi import FastAPI

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

from frontend.meetings_styles import BREAKPILOT_STYLES, ICONS
from frontend.meetings_templates import render_sidebar, render_base_page
from frontend.meetings import router


# Create test app
app = FastAPI()
app.include_router(router)
client = TestClient(app)


class TestMeetingsStyles:
    """Test CSS styles and icons"""

    def test_breakpilot_styles_exists(self):
        """Test that BREAKPILOT_STYLES is defined"""
        assert BREAKPILOT_STYLES is not None
        assert isinstance(BREAKPILOT_STYLES, str)
        assert len(BREAKPILOT_STYLES) > 0

    def test_breakpilot_styles_contains_css_variables(self):
        """Test that CSS contains required variables"""
        assert "--bp-primary:" in BREAKPILOT_STYLES
        assert "--bp-bg:" in BREAKPILOT_STYLES
        assert "--bp-surface:" in BREAKPILOT_STYLES
        assert "--bp-text:" in BREAKPILOT_STYLES

    def test_breakpilot_styles_contains_layout_classes(self):
        """Test that CSS contains layout classes"""
        assert ".app-container" in BREAKPILOT_STYLES
        assert ".sidebar" in BREAKPILOT_STYLES
        assert ".main-content" in BREAKPILOT_STYLES

    def test_icons_exists(self):
        """Test that ICONS dictionary is defined"""
        assert ICONS is not None
        assert isinstance(ICONS, dict)

    def test_icons_contains_required_icons(self):
        """Test that required icons are present"""
        required_icons = ['home', 'video', 'calendar', 'graduation', 'record', 'grid', 'external', 'users', 'plus']
        for icon in required_icons:
            assert icon in ICONS, f"Missing icon: {icon}"

    def test_icons_are_svg(self):
        """Test that icons are SVG strings"""
        for name, svg in ICONS.items():
            assert isinstance(svg, str), f"Icon {name} is not a string"
            assert "<svg" in svg or "svg" in svg.lower(), f"Icon {name} does not appear to be SVG"


class TestMeetingsTemplates:
    """Test template rendering functions"""

    def test_render_sidebar_returns_string(self):
        """Test that render_sidebar returns a string"""
        result = render_sidebar()
        assert isinstance(result, str)

    def test_render_sidebar_contains_navigation(self):
        """Test that sidebar contains navigation items"""
        result = render_sidebar()
        assert "sidebar" in result
        assert "/meetings" in result
        assert "Dashboard" in result

    def test_render_sidebar_active_page_dashboard(self):
        """Test active page highlighting for dashboard"""
        result = render_sidebar("dashboard")
        # The active class should be applied to dashboard link
        assert "active" in result

    def test_render_sidebar_active_page_schedule(self):
        """Test active page highlighting for schedule"""
        result = render_sidebar("schedule")
        assert "active" in result
        assert "Termine" in result

    def test_render_sidebar_contains_all_nav_items(self):
        """Test that all navigation items are present"""
        result = render_sidebar()
        nav_items = ["Dashboard", "Aktive Meetings", "Termine", "Schulungen", "Aufzeichnungen", "Breakout-Rooms"]
        for item in nav_items:
            assert item in result, f"Missing nav item: {item}"

    def test_render_sidebar_contains_external_links(self):
        """Test that sidebar contains external links"""
        result = render_sidebar()
        assert "/studio" in result
        assert "/school" in result
        assert "Zurück zum Studio" in result

    def test_render_base_page_returns_html(self):
        """Test that render_base_page returns HTML"""
        result = render_base_page("Test Title", "<p>Test Content</p>")
        assert isinstance(result, str)
        assert "<!DOCTYPE html>" in result
        assert "</html>" in result

    def test_render_base_page_contains_title(self):
        """Test that title is included in HTML"""
        result = render_base_page("My Test Page", "<p>Content</p>")
        assert "My Test Page" in result
        assert "<title>BreakPilot Meet – My Test Page</title>" in result

    def test_render_base_page_contains_content(self):
        """Test that content is included in HTML"""
        test_content = "<p>This is test content</p>"
        result = render_base_page("Title", test_content)
        assert test_content in result

    def test_render_base_page_includes_styles(self):
        """Test that styles are included"""
        result = render_base_page("Title", "Content")
        assert "<style>" in result
        assert "--bp-primary" in result

    def test_render_base_page_includes_sidebar(self):
        """Test that sidebar is included"""
        result = render_base_page("Title", "Content", "dashboard")
        assert "sidebar" in result
        assert "BreakPilot Meet" in result

    def test_render_base_page_active_page_passed(self):
        """Test that active_page is passed to sidebar"""
        result = render_base_page("Title", "Content", "trainings")
        assert "Schulungen" in result


class TestMeetingsRoutes:
    """Test route handlers"""

    def test_meetings_dashboard_returns_200(self):
        """Test dashboard route returns 200"""
        response = client.get("/meetings")
        assert response.status_code == 200

    def test_meetings_dashboard_returns_html(self):
        """Test dashboard returns HTML content"""
        response = client.get("/meetings")
        assert "text/html" in response.headers.get("content-type", "")
        assert "Meeting Dashboard" in response.text

    def test_active_meetings_returns_200(self):
        """Test active meetings route returns 200"""
        response = client.get("/meetings/active")
        assert response.status_code == 200

    def test_schedule_meetings_returns_200(self):
        """Test schedule meetings route returns 200"""
        response = client.get("/meetings/schedule")
        assert response.status_code == 200

    def test_trainings_page_returns_200(self):
        """Test trainings page returns 200"""
        response = client.get("/meetings/trainings")
        assert response.status_code == 200

    def test_recordings_page_returns_200(self):
        """Test recordings page returns 200"""
        response = client.get("/meetings/recordings")
        assert response.status_code == 200

    def test_breakout_rooms_page_returns_200(self):
        """Test breakout rooms page returns 200"""
        response = client.get("/meetings/breakout")
        assert response.status_code == 200

    def test_meeting_room_returns_200(self):
        """Test meeting room route returns 200"""
        response = client.get("/meetings/room/test-room-123")
        assert response.status_code == 200
        assert "test-room-123" in response.text

    def test_quick_meeting_returns_redirect(self):
        """Test quick meeting creates redirect"""
        response = client.get("/meetings/quick", follow_redirects=False)
        # Should redirect to a new room
        assert response.status_code in [302, 307, 200]

    def test_parent_teacher_meeting_returns_200(self):
        """Test parent-teacher meeting page returns 200"""
        response = client.get("/meetings/parent-teacher")
        assert response.status_code == 200
        assert "Elterngespräch" in response.text or "Elterngespr" in response.text or "Parent" in response.text


class TestMeetingsIntegration:
    """Integration tests for meetings module"""

    def test_all_pages_have_consistent_structure(self):
        """Test that all pages have consistent HTML structure"""
        pages = [
            "/meetings",
            "/meetings/active",
            "/meetings/schedule",
            "/meetings/trainings",
            "/meetings/recordings",
            "/meetings/breakout",
        ]
        for page in pages:
            response = client.get(page)
            assert response.status_code == 200
            assert "<!DOCTYPE html>" in response.text
            assert "app-container" in response.text
            assert "sidebar" in response.text
            assert "main-content" in response.text

    def test_all_pages_have_navigation(self):
        """Test that all pages have navigation"""
        pages = [
            "/meetings",
            "/meetings/active",
            "/meetings/schedule",
        ]
        for page in pages:
            response = client.get(page)
            assert "/studio" in response.text
            assert "/school" in response.text
