from core.database.abc.db_model_dao_abc import DbModelDaoABC
from core.logger import DBLogger
from data.schemas.public.user_space_user import UserSpaceUser

logger = DBLogger(__name__)


class UserSpaceUserDao(DbModelDaoABC[UserSpaceUser]):
    def __init__(self):
        DbModelDaoABC.__init__(
            self, __name__, UserSpaceUser, "public.user_spaces_users"
        )
        self.attribute(UserSpaceUser.user_space_id, int)
        self.attribute(UserSpaceUser.user_id, int)
        self.attribute(UserSpaceUser.invitation, str)

    async def get_by_user_space_id(
        self, user_space_id: str, with_deleted=False
    ) -> list[UserSpaceUser]:
        f = [
            {UserSpaceUser.user_space_id: user_space_id},
            {UserSpaceUser.invitation: None},
        ]
        if not with_deleted:
            f.append({UserSpaceUser.deleted: False})

        return await self.find_by(f)

    async def get_by_user_id(
        self, user_id: str, with_deleted=False
    ) -> list[UserSpaceUser]:
        f = [{UserSpaceUser.user_id: user_id}, {UserSpaceUser.invitation: None}]
        if not with_deleted:
            f.append({UserSpaceUser.deleted: False})

        return await self.find_by(f)


userSpaceUserDao = UserSpaceUserDao()
