"""Simple notifier bridge for UI notifications.

Modules can call `notify(level, message)` to emit operator-visible
notifications. A UI (TUI) may register a callback via `register_callback()`
to receive notifications and display them. If no callback is registered,
notifications are logged.
"""
import logging
from typing import Callable, Optional

_callback: Optional[Callable[[str, str], None]] = None


def register_callback(cb: Callable[[str, str], None]) -> None:
    """Register a callback to receive notifications.

    Callback receives (level, message).
    """
    global _callback
    _callback = cb


def notify(level: str, message: str) -> None:
    """Emit a notification. If UI callback registered, call it; otherwise log."""
    global _callback
    if _callback:
        try:
            _callback(level, message)
            return
        except Exception:
            logging.getLogger(__name__).exception("Notifier callback failed")

    # Fallback to logging
    log = logging.getLogger("pentestagent.notifier")
    if level.lower() in ("error", "critical"):
        log.error(message)
    elif level.lower() in ("warn", "warning"):
        log.warning(message)
    else:
        log.info(message)
