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_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_id = user_id

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

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

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

    @async_property
    async def user(self):
        if self._user_id is None:
            return None

        from data.schemas.administration.user_dao import userDao

        user = await userDao.get_by_id(self.user_id)
        return user
