"""Registry de módulos do Ligbox Ops Desk (Spec 015)."""

from __future__ import annotations

from dataclasses import dataclass


@dataclass(frozen=True)
class ModuleDef:
    id: str
    label: str
    description: str
    locked: bool = False
    nav_views: tuple[str, ...] = ()
    default_enabled: bool = True


MODULES: tuple[ModuleDef, ...] = (
    ModuleDef(
        id="core",
        label="Núcleo",
        description="Dashboard, tickets, autenticação e conta.",
        locked=True,
        nav_views=("dashboard", "tickets", "account"),
    ),
    ModuleDef(
        id="overview",
        label="Audit Overview",
        description="Visão clássica por tenant e domínio.",
        nav_views=("overview",),
    ),
    ModuleDef(
        id="overview-home",
        label="Serviços",
        description="Orquestração de serviços — clientes, catálogo cPanel e purge OPS (Spec 018).",
        nav_views=("overview-home",),
    ),
    ModuleDef(
        id="infra",
        label="Infra",
        description="Health VM112, VM104 e integrações técnicas.",
        nav_views=("infra",),
    ),
    ModuleDef(
        id="infra2-soc",
        label="Infra 2 SOC",
        description="Painel visual SOC VM112→VM122.",
        nav_views=("infra2",),
    ),
    ModuleDef(
        id="funnel-timing",
        label="Relógio por fase",
        description="Duração entre etapas do onboarding VM112.",
        nav_views=(),
    ),
    ModuleDef(
        id="wizard-security",
        label="Segurança Wizard",
        description="CSP, auditoria de inputs e telemetria cibersegurança VM112 (Spec 021).",
        nav_views=(),
        default_enabled=True,
    ),
    ModuleDef(
        id="carbonio-release",
        label="Bloqueios Carbonio",
        description="Libertar e-mail ACCOUNT_EXISTS — zmprov da via VM112 (Spec 022).",
        nav_views=(),
        default_enabled=True,
    ),
    ModuleDef(
        id="vm112-domains",
        label="Domínios VM112",
        description="Account Home — domínios orquestrados e purge (testes).",
        nav_views=(),
        default_enabled=True,
    ),
    ModuleDef(
        id="billing-recurrence",
        label="Cobrança recorrente",
        description="KPI billing, conta cliente e links financeiro (Spec 023).",
        nav_views=(),
        default_enabled=True,
    ),
    ModuleDef(
        id="email-migration",
        label="Migração E-mail",
        description="Jobs imapsync legado → Carbonio + gate DNS (Spec 019).",
        nav_views=("email-migration",),
        default_enabled=True,
    ),
    ModuleDef(
        id="wazuh-soc",
        label="Wazuh SOC Overview",
        description="Card e modal de alertas VM104 no Overview.",
        nav_views=(),
    ),
    ModuleDef(
        id="leads",
        label="Leads abandonados",
        description="CRM de sessões stale do funil.",
        nav_views=("leads",),
    ),
    ModuleDef(
        id="events",
        label="Eventos webhook",
        description="Feed bruto de webhooks VM112 e Wazuh.",
        nav_views=("events",),
    ),
    ModuleDef(
        id="tenants",
        label="Tenants",
        description="Registo de nós Ligbox.",
        nav_views=("tenants",),
    ),
    ModuleDef(
        id="messages",
        label="Mensagens",
        description="Pedidos de cadastro de administradores.",
        nav_views=("messages",),
    ),
    ModuleDef(
        id="admin-users",
        label="Administradores",
        description="Gestão de utilizadores Desk.",
        nav_views=("admin",),
    ),
    ModuleDef(
        id="modules-admin",
        label="Módulos",
        description="Activar/desactivar módulos do Desk.",
        locked=True,
        nav_views=("modules",),
    ),
)

MODULE_BY_ID = {m.id: m for m in MODULES}

# Spec 027 — módulos ON por defeito na activação (secção 7)
ROLE_MODULE_DEFAULTS: dict[str, frozenset[str]] = {
    "sales_admin": frozenset(
        {"core", "leads", "funnel-timing", "overview-home", "billing-recurrence", "tenants"}
    ),
    "sales_support": frozenset({"core", "leads", "funnel-timing", "overview-home", "tenants"}),
    "finance": frozenset({"core", "overview-home", "billing-recurrence", "events"}),
    "marketing": frozenset({"core", "leads", "funnel-timing", "overview-home"}),
    "seo": frozenset({"core", "funnel-timing", "overview-home", "leads"}),
    "developer": frozenset({"core", "events", "infra", "overview"}),
    "devops": frozenset({"core", "infra", "infra2-soc", "overview-home", "events"}),
    "security_analyst": frozenset({"core", "infra2-soc", "wazuh-soc", "events"}),
    "content_editor": frozenset({"core"}),
    "agentic_operator": frozenset({"core", "overview", "events", "infra2-soc"}),
}


def role_module_defaults(role: str) -> frozenset[str] | None:
    """None = roles ops legacy (003) — respeitam só toggles globais."""
    if role in ("super_admin", "ops_lead", "technician", "noc"):
        return None
    return ROLE_MODULE_DEFAULTS.get(role, frozenset({"core"}))


def all_module_ids() -> list[str]:
    return [m.id for m in MODULES]
