# app/src/notifications/service.py

from __future__ import annotations

from src.notifications.channels.telegram import TelegramNotificationChannel
from src.notifications.dedupe import NotificationDedupe
from src.notifications.models import NotificationMessage
from src.notifications.templates.execution import build_execution_notification
from src.notifications.templates.signal import build_signal_notification
from src.runtime_events.models import RuntimeEvent


class NotificationService:
    async def handle_runtime_event(self, event: RuntimeEvent) -> None:
        message = self._build_message(event)

        if message is None:
            return

        if not NotificationDedupe.should_send(message.dedupe_key):
            return

        await TelegramNotificationChannel().send(message)

    def _build_message(self, event: RuntimeEvent) -> NotificationMessage | None:
        return (
            build_signal_notification(event)
            or build_execution_notification(event)
        )