from typing import override

from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession

from template_project.adapters.data_gateways.tables import notification_device_table
from template_project.application.notification_device.data_gateway import NotificationDeviceDataGateway
from template_project.application.notification_device.entity import NotificationDevice
from template_project.application.user.entity import UserId


class DefaultNotificationDeviceDataGateway(NotificationDeviceDataGateway):
    def __init__(self, session: AsyncSession) -> None:
        self._session = session

    @override
    async def load_by_user_id(self, user_id: UserId) -> NotificationDevice | None:
        statement = select(NotificationDevice).where(notification_device_table.c.user_id == user_id)
        result = await self._session.execute(statement)
        return result.scalar_one_or_none()

    @override
    async def load_by_user_id_and_device_id(
        self,
        user_id: UserId,
        device_id: str,
    ) -> NotificationDevice | None:
        statement = select(NotificationDevice).where(
            notification_device_table.c.user_id == user_id,
            notification_device_table.c.device_id == device_id,
        )
        result = await self._session.execute(statement)
        return result.scalar_one_or_none()
