"""
DummyBackend – in-memory backend for testing and development.

Records every sent payload so tests can assert on it without
making real network calls.
"""
from __future__ import annotations

import logging
from typing import TYPE_CHECKING, Any

from django_notify_hub.backends.base import AbstractNotificationBackend
from django_notify_hub.types import NotificationChannel

if TYPE_CHECKING:
    from django_notify_hub.types import NotificationPayload

logger = logging.getLogger("django_notify_hub")


class DummyBackend(AbstractNotificationBackend):
    """
    No-op backend that stores sent payloads in memory.

    Usage in tests::

        from django_notify_hub.backends.dummy import DummyBackend
        backend = DummyBackend()
        # ... trigger notifications ...
        assert len(backend.sent) == 1
        assert backend.sent[0]["payload"].get_title() == "Hello"
    """

    channel = NotificationChannel.DUMMY

    def setup(self, **kwargs: Any) -> None:
        # Each instance gets its own sent-log so tests are isolated.
        self.sent: list[dict[str, Any]] = []

    def _deliver(self, payload: "NotificationPayload", recipient: Any) -> bool:
        entry = {"payload": payload, "recipient": recipient}
        self.sent.append(entry)
        logger.info(
            "[DummyBackend] Notification delivered to %s | title=%s",
            getattr(recipient, "pk", recipient),
            payload.get_title(),
        )
        return True

    def _deliver_many(
        self, payload: "NotificationPayload", recipients: Any
    ) -> int:
        for recipient in recipients:
            self._deliver(payload, recipient)
        return len(self.sent)

    def reset(self) -> None:
        """Clear the sent log between tests."""
        self.sent.clear()

