# app/src/notifications/dedupe.py

from __future__ import annotations

import time


class NotificationDedupe:
    _sent_at_by_key: dict[str, float] = {}

    @classmethod
    def should_send(cls, key: str | None, *, ttl_seconds: int = 120) -> bool:
        if not key:
            return True

        now = time.monotonic()
        last_sent_at = cls._sent_at_by_key.get(key)

        if last_sent_at is not None and now - last_sent_at < ttl_seconds:
            return False

        cls._sent_at_by_key[key] = now
        return True