"""Navigation model and renderer for the ClientFlow admin UI."""
from __future__ import annotations

from dataclasses import dataclass

from app.admin_ui.components import esc


@dataclass(frozen=True)
class NavItem:
    key: str
    url: str
    icon: str
    label: str
    area: str = "primary"


PRIMARY_NAV_ITEMS: tuple[NavItem, ...] = (
    NavItem("overview", "/", "bi-speedometer2", "Dashboard"),
    NavItem("operations", "/operations", "bi-check2-square", "Centro de trabalho"),
    NavItem("reconciliation", "/reconciliation", "bi-diagram-3", "Reconciliação"),
    NavItem("opportunities", "/opportunities", "bi-funnel", "Oportunidades"),
    NavItem("forecast", "/forecast", "bi-graph-up-arrow", "Metas e previsão"),
    NavItem("customers", "/customers", "bi-people", "Clientes"),
    NavItem("products", "/products", "bi-box-seam", "Produtos"),
    NavItem("orders", "/orders", "bi-truck", "Encomendas"),
    NavItem("finance", "/finance", "bi-file-earmark-text", "Financeiro"),
    NavItem("integrations", "/integrations", "bi-puzzle", "Integrações"),
)

ADMIN_NAV_ITEMS: tuple[NavItem, ...] = (
    NavItem("tasks", "/tasks", "bi-list-check", "Tasks", "admin"),
    NavItem("communications", "/communications", "bi-envelope", "Comunicações", "admin"),
    NavItem("outbox", "/outbox", "bi-send", "Outbox", "admin"),
    NavItem("events", "/events", "bi-activity", "Eventos", "admin"),
    NavItem("runs", "/runs", "bi-cpu", "Runs", "admin"),
    NavItem("queues", "/queues", "bi-diagram-3", "Filas", "admin"),
    NavItem("system", "/system/health", "bi-heart-pulse", "System health", "admin"),
    NavItem("settings", "/settings", "bi-gear", "Configuração", "admin"),
)

ADMIN_ACTIVE_KEYS = frozenset(item.key for item in ADMIN_NAV_ITEMS)


def _render_items(items: tuple[NavItem, ...], active: str) -> str:
    links = ""
    for item in items:
        css = "cf-nav-link active" if item.key == active else "cf-nav-link"
        links += f"""
        <a class="{css}" href="{esc(item.url)}">
          <span class="cf-nav-icon"><i class="bi {esc(item.icon)}"></i></span>
          <span>{esc(item.label)}</span>
        </a>
        """
    return links


def nav(active: str = "") -> str:
    """Render the operator-first navigation introduced in v4.7.

    Primary navigation stays focused on daily work. Technical pages remain
    available under Admin, without removing their public URLs.
    """
    active = active or "overview"
    admin_active = " active" if active in ADMIN_ACTIVE_KEYS else ""
    admin_open = " open" if active in ADMIN_ACTIVE_KEYS else ""
    primary_links = _render_items(PRIMARY_NAV_ITEMS, active)
    admin_links = _render_items(ADMIN_NAV_ITEMS, active)

    return f"""
    <aside class="cf-sidebar">
      <a class="cf-brand" href="/">
        <span class="cf-brand-mark">CF</span>
        <span class="cf-brand-text"><strong>ClientFlow</strong><small>v4.7 Operator Workbench</small></span>
      </a>

      <div class="cf-nav-title">Principal</div>
      <nav class="cf-nav" aria-label="Navegação principal">
        {primary_links}
      </nav>

      <div class="cf-nav-title">Admin</div>
      <details class="cf-admin-nav"{admin_open}>
        <summary class="cf-nav-link{admin_active}">
          <span class="cf-nav-icon"><i class="bi bi-tools"></i></span>
          <span>Admin</span>
        </summary>
        <nav class="cf-nav cf-nav-admin" aria-label="Navegação admin">
          {admin_links}
        </nav>
      </details>

      <div class="cf-sidebar-footer">
        <div class="cf-company">Empresa<strong>BLIF / ClientFlow</strong></div>
        <div class="cf-user-mini">
          <div class="cf-avatar">CF</div>
          <div>
            <strong>Operador</strong>
            <span>Admin</span>
          </div>
        </div>
      </div>
    </aside>
    """
