from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]


def read(path: str) -> str:
    return (ROOT / path).read_text(encoding="utf-8")


def test_v47_admin_ui_modules_exist_and_dashboard_is_smaller():
    assert (ROOT / "app/admin_ui/styles.py").exists()
    assert (ROOT / "app/admin_ui/navigation.py").exists()
    assert (ROOT / "app/admin_ui/layout.py").exists()
    admin_lines = read("app/admin_dashboard.py").count("\n") + 1
    assert admin_lines < 4500
    assert "ADMIN_UI_CSS moved to app.admin_ui.styles" in read("app/admin_dashboard.py")


def test_v47_primary_navigation_is_operator_first_and_admin_is_separate():
    navigation = read("app/admin_ui/navigation.py")
    primary = navigation[navigation.index("PRIMARY_NAV_ITEMS"):navigation.index("ADMIN_NAV_ITEMS")]
    admin = navigation[navigation.index("ADMIN_NAV_ITEMS"):]
    for label in ["Dashboard", "Centro de trabalho", "Oportunidades", "Clientes", "Produtos", "Encomendas", "Financeiro", "Integrações"]:
        assert label in primary
    for technical in ["Tasks", "Comunicações", "Outbox", "Eventos", "Runs", "Filas", "System health"]:
        assert technical not in primary
        assert technical in admin
    assert "v4.7 Operator Workbench" in navigation


def test_v47_layout_uses_shared_navigation_and_css_endpoint_keeps_compatibility():
    layout = read("app/admin_ui/layout.py")
    dashboard = read("app/admin_dashboard.py")
    styles = read("app/admin_ui/styles.py")
    assert "from app.admin_ui.navigation import nav" in layout
    assert "from app.admin_ui.styles import ADMIN_UI_V451_CSS" in layout
    assert "return Response(ADMIN_UI_V451_CSS" in dashboard
    assert "v4.7 admin navigation" in styles


def test_v47_opportunities_board_has_direct_pending_task_cta():
    opportunities = read("app/admin_ui/pages/opportunities.py")
    assert 'cta_label = "Concluir tarefa pendente" if pending else "Ver oportunidade"' in opportunities
    assert "btn-primary" in opportunities
    assert "Pipeline comercial com foco na próxima ação" in opportunities


def test_v47_documentation_describes_operator_model_and_no_db_migration():
    doc = read("docs/CLIENTFLOW_V47_ADMIN_UI_REFACTOR.md")
    assert "Chatwoot" in doc and "Operations" in doc
    assert "Admin" in doc and "Outbox" in doc
    assert "não requer migração de base de dados" in doc


def test_v471_sidebar_admin_menu_can_scroll_when_open_on_short_screens():
    styles = read("app/admin_ui/styles.py")
    assert "v4.7.1 admin navigation overflow fix" in styles
    assert ".cf-sidebar" in styles
    assert "overflow-y:auto" in styles
    assert "max-height:100vh" in styles
    assert "overscroll-behavior:contain" in styles
    assert "scrollbar-gutter:stable" in styles
    assert "overflow-y:hidden" in styles  # mobile horizontal nav keeps old behavior
