"""Notification type registry.

Register notification types with their default channel configuration so
services and preferences UIs can discover all known notification kinds.

Usage::

    from simorgh.apps.notifications.registry import register_notification_type, NotificationTypeSpec

    register_notification_type(NotificationTypeSpec(
        code="workflow.approval_needed",
        label="Approval Needed",
        default_channels=["inbox", "email"],
        is_critical=False,
    ))
"""

from __future__ import annotations

from dataclasses import dataclass, field


@dataclass
class NotificationTypeSpec:
    """Descriptor for a known notification type."""

    code: str
    """Dot-separated kind key, e.g. 'workflow.approval_needed'."""

    label: str
    """Human-readable label used in preference UIs."""

    default_channels: list[str] = field(default_factory=lambda: ["inbox"])
    """Channels to deliver on when no user preference overrides."""

    is_critical: bool = False
    """Critical notifications are always delivered — users cannot opt out."""


_registry: dict[str, NotificationTypeSpec] = {}


def register_notification_type(spec: NotificationTypeSpec) -> None:
    """Register a notification type. Safe to call at import time."""
    _registry[spec.code] = spec


def get_notification_type(code: str) -> NotificationTypeSpec | None:
    """Return the spec for *code*, or None if not registered."""
    return _registry.get(code)


def get_all_notification_types() -> list[NotificationTypeSpec]:
    """Return all registered specs ordered by code."""
    return sorted(_registry.values(), key=lambda s: s.code)


# ---------------------------------------------------------------------------
# Built-in platform notification types
# ---------------------------------------------------------------------------

_BUILTIN: list[NotificationTypeSpec] = [
    NotificationTypeSpec(
        code="workflow.approval_needed",
        label="Approval Needed",
        default_channels=["inbox", "email"],
    ),
    NotificationTypeSpec(
        code="workflow.approved",
        label="Approved",
        default_channels=["inbox"],
    ),
    NotificationTypeSpec(
        code="workflow.rejected",
        label="Rejected",
        default_channels=["inbox", "email"],
    ),
    NotificationTypeSpec(
        code="helpdesk.ticket_assigned",
        label="Ticket Assigned to You",
        default_channels=["inbox", "email"],
    ),
    NotificationTypeSpec(
        code="helpdesk.ticket_reply",
        label="New Ticket Reply",
        default_channels=["inbox", "email"],
    ),
    NotificationTypeSpec(
        code="helpdesk.ticket_status_changed",
        label="Ticket Status Changed",
        default_channels=["inbox"],
    ),
    NotificationTypeSpec(
        code="crm.lead_assigned",
        label="Lead Assigned to You",
        default_channels=["inbox"],
    ),
    NotificationTypeSpec(
        code="crm.activity_reminder",
        label="Activity Reminder",
        default_channels=["inbox", "email"],
    ),
    NotificationTypeSpec(
        code="dms.document_shared",
        label="Document Shared with You",
        default_channels=["inbox"],
    ),
    NotificationTypeSpec(
        code="dms.document_comment",
        label="Document Comment",
        default_channels=["inbox"],
    ),
    NotificationTypeSpec(
        code="auth.login_new_device",
        label="New Device Login",
        default_channels=["inbox", "email"],
        is_critical=True,
    ),
    NotificationTypeSpec(
        code="auth.password_changed",
        label="Password Changed",
        default_channels=["inbox", "email"],
        is_critical=True,
    ),
]

for _spec in _BUILTIN:
    register_notification_type(_spec)
