from datetime import datetime
from typing import Optional

from async_property import async_property

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


class Group(DbModelABC):
    def __init__(
        self,
        id: SerialId,
        name: str,
        user_space_id: Optional[SerialId] = None,
        deleted: bool = False,
        editor_id: Optional[SerialId] = None,
        created: Optional[datetime] = None,
        updated: Optional[datetime] = None,
    ):
        DbModelABC.__init__(self, id, deleted, editor_id, created, updated)
        self._name = name
        self._user_space_id = user_space_id

    @property
    def name(self) -> str:
        return self._name

    @name.setter
    def name(self, value: str):
        self._name = value

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

    @async_property
    async def user_space(self):
        if self._user_space_id is None:
            return None

        from data.schemas.public.user_space_dao import userSpaceDao

        return await userSpaceDao.get_by_id(self._user_space_id)
