from template_project.application.common.data_structure import to_data_structure
from template_project.application.common.identity_provider import IdentityProvider
from template_project.application.common.interactor import to_interactor
from template_project.application.common.notifications.service import NotificationService
from template_project.application.notification_device.data_gateway import NotificationDeviceDataGateway
from template_project.application.notification_device.errors import NotificationDeviceNotFoundError


@to_data_structure
class SendNotificationRequest:
    title: str
    body: str


@to_interactor
class NotificationInteractor:
    identity_provider: IdentityProvider
    notification_device_data_gateway: NotificationDeviceDataGateway
    notification_service: NotificationService

    async def send_notification(self, request: SendNotificationRequest) -> None:
        current_user = await self.identity_provider.get_current_user()
        notification_device = await self.notification_device_data_gateway.load_by_user_id(current_user.id)

        if not notification_device:
            raise NotificationDeviceNotFoundError

        await self.notification_service.send_notification(
            identifier=notification_device.device_id,
            title=request.title,
            body=request.body,
        )
