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_v45_communications_service_and_schema_exist():
    service = read("app/communication_service.py")
    db = read("app/db.py")
    migration = read("migrations/006_v45_operational_core.sql")

    assert "CREATE TABLE IF NOT EXISTS communications" in service
    assert "CREATE TABLE IF NOT EXISTS timeline_events" in service
    assert "communication_id" in service
    assert "ensure_communication_schema" in db
    assert "communications" in migration
    assert "timeline_events" in migration


def test_v45_admin_ui_routes_are_registered():
    communications = read("app/admin_ui/pages/communications.py")
    operations = read("app/admin_ui/pages/operations.py")
    dashboard = read("app/admin_ui/pages/dashboard.py")
    assert '@router.get("/communications"' in communications
    assert '@router.get("/comunicacoes"' in communications
    assert '@router.get("/communications/{communication_id}"' in communications
    assert 'Centro de trabalho' in operations
    assert 'Dashboard = visibilidade' in dashboard
    assert 'Inbox classificada' in communications


def test_v45_operations_surface_work_items():
    operations = read("app/operations_service.py")
    operations_page = read("app/admin_ui/pages/operations.py")
    assert "work_items" in operations
    assert "recent_communications" in operations
    assert "communications_open" in operations
    assert "Fila operacional priorizada" in operations_page


def test_v45_opportunity_has_communication_and_timeline_sections():
    opportunities = read("app/admin_ui/pages/opportunities.py")
    assert 'href="#comunicacoes"' in opportunities
    assert 'id="comunicacoes"' in opportunities
    assert 'id="timeline"' in opportunities
    assert 'list_communications_for_opportunity' in opportunities
