from datetime import datetime
from typing import Optional

from async_property import async_property

from core.database.abc.db_join_model_abc import DbJoinModelABC
from core.database.abc.db_model_abc import DbModelABC
from core.typing import SerialId


class UserSpaceUser(DbJoinModelABC):
    def __init__(
        self,
        id: SerialId,
        user_space_id: SerialId,
        user_id: SerialId,
        invitation: Optional[str] = None,
        deleted: bool = False,
        editor_id: Optional[SerialId] = None,
        created: Optional[datetime] = None,
        updated: Optional[datetime] = None,
    ):
        DbJoinModelABC.__init__(
            self, id, user_space_id, user_id, deleted, editor_id, created, updated
        )
        self._user_space_id = user_space_id
        self._user_id = user_id
        self._invitation = invitation

    @property
    def user_space_id(self) -> SerialId:
        return self._user_space_id

    @async_property
    async def user_space(self):
        from data.schemas.public.user_space_dao import userSpaceDao

        return await userSpaceDao.get_by_id(self._user_space_id)

    @property
    def user_id(self) -> SerialId:
        return self._user_id

    @async_property
    async def user(self):
        from data.schemas.administration.user_dao import userDao

        return await userDao.get_by_id(self._user_id)

    @property
    def invitation(self) -> Optional[str]:
        return self._invitation

    @invitation.setter
    def invitation(self, value: Optional[str]):
        self._invitation = value
