"""Thin wrapper around core.notifications for SME-Hub verbs.

Centralises the relative URL resolution and verb-default channels so callers in
each SME-Hub submodule do not repeat the boilerplate.

A second responsibility: the inventory function ``smehub_verbs`` returns the
canonical set of SME-Hub-prefixed verbs. The Phase 0 test suite uses it to
assert that each verb is reachable through ``send_notification`` and (when
appropriate) has an active ``NotificationTemplate`` row.
"""
from __future__ import annotations

from collections.abc import Iterable

from apps.core.notifications.models import Notification, NotificationPreference
from apps.core.notifications.services import bulk_notify, send_notification

Verb = Notification.Verb
Channel = NotificationPreference.Channel


def smehub_verbs() -> list[str]:
    """All Notification.Verb values that belong to the SME-Hub surface."""
    return [v.value for v in Notification.Verb if v.value.startswith("smehub_")]


def notify_smehub(
    recipient,
    message: str,
    *,
    verb: str,
    action_url: str = "",
    target=None,
    channels: list[str] | None = None,
    email_context: dict | None = None,
) -> Notification:
    """Send a single SME-Hub notification with the in-app + email defaults."""
    extra: dict = {}
    if action_url:
        extra["action_url"] = action_url
    if target is not None:
        from django.contrib.contenttypes.models import ContentType

        extra["content_type"] = ContentType.objects.get_for_model(target.__class__)
        extra["object_id"] = str(target.pk)
    return send_notification(
        recipient,
        message,
        verb=verb,
        channels=channels or [Channel.EMAIL, Channel.IN_APP],
        email_context=email_context,
        **extra,
    )


def bulk_notify_smehub(
    recipients: Iterable,
    message: str,
    *,
    verb: str,
    action_url: str = "",
    email_context: dict | None = None,
) -> None:
    bulk_notify(
        list(recipients),
        message,
        verb=verb,
        email_context=email_context,
        action_url=action_url,
    )
