"""Frontend E2E tests — navigation (tabs, drawer)."""

import pytest


def _login(page):
    """Helper: login as admin."""
    page.locator("#loginUsername").fill("admin")
    page.locator("#loginPassword").fill("changeme")
    page.locator("button:has-text('Sign In')").click()
    page.wait_for_selector("#loginOverlay", state="hidden", timeout=5000)


@pytest.mark.frontend
def test_tab_navigation(page, live_server):
    """Clicking bottom tabs switches the active panel."""
    _login(page)

    # Initially the Add Asset tab is active (it's the default)
    assert page.locator("#tabAddAsset.tab-panel.active").is_visible()

    # Click "Assets" tab (📦 Assets)
    page.locator(".tab-btn[data-tab='tabAssets']").click()
    assert page.locator("#tabAssets.tab-panel.active").is_visible()

    # Click "Dashboard" tab (📊 Dash)
    page.locator(".tab-btn[data-tab='tabDashboard']").click()
    assert page.locator("#tabDashboard.tab-panel.active").is_visible()

    # Click back to Add Asset
    page.locator(".tab-btn[data-tab='tabAddAsset']").click()
    assert page.locator("#tabAddAsset.tab-panel.active").is_visible()


@pytest.mark.frontend
def test_drawer_open_close(page, live_server):
    """Hamburger opens drawer, close button closes it."""
    _login(page)

    # Open drawer
    page.locator(".hamburger").click()
    page.wait_for_selector("#drawer.open", timeout=3000)
    assert page.locator("#drawer.open").is_visible()

    # Close drawer via ✕ button
    page.locator(".close-drawer").click()
    # Drawer should lose .open class
    page.wait_for_selector("#drawer:not(.open)", timeout=3000)


@pytest.mark.frontend
def test_drawer_navigation(page, live_server):
    """Drawer links switch tabs and close the drawer."""
    _login(page)

    # Open drawer
    page.locator(".hamburger").click()
    page.wait_for_selector("#drawer.open", timeout=3000)

    # Click "Asset List" in drawer (📦 Asset List)
    page.locator(".dn-item[data-tab='tabAssets']").click()
    page.wait_for_selector("#tabAssets.active", timeout=3000)
    assert page.locator("#tabAssets.tab-panel.active").is_visible()
    # Drawer should close after navigation
    assert not page.locator("#drawer.open").is_visible()


@pytest.mark.frontend
def test_drawer_user_info(page, live_server):
    """Drawer shows current user info."""
    _login(page)

    page.locator(".hamburger").click()
    page.wait_for_selector("#drawer.open", timeout=3000)

    assert page.locator("#drawerName").inner_text() == "admin"
    assert "admin" in page.locator("#drawerRole").inner_text().lower()
