from api_graphql.abc.db_model_query_abc import DbModelQueryABC
from data.schemas.public.group_dao import groupDao
from data.schemas.public.short_url_dao import shortUrlDao
from data.schemas.public.user_space import UserSpace
from data.schemas.public.user_space_dao import userSpaceDao


class UserSpaceQuery(DbModelQueryABC):
    def __init__(self):
        DbModelQueryABC.__init__(self, "UserSpace", userSpaceDao, with_history=True)

        self.set_field("name", lambda x, *_: x.name)
        self.set_field("owner", lambda x, *_: x.owner)
        self.set_field("users", self._get_users)

        self.set_history_reference_dao(groupDao, "userspaceid")
        self.set_history_reference_dao(shortUrlDao, "userspaceid")

    @staticmethod
    async def _get_users(space: UserSpace, *_):
        return await userSpaceDao.get_users(space.id, with_invitations=False)
