from datetime import datetime
from typing import Optional

from async_property import async_property

from core.typing import SerialId
from core.database.abc.db_model_abc import DbModelABC
from data.schemas.permission.permission_dao import permissionDao
from data.schemas.permission.role_dao import roleDao


class RolePermission(DbModelABC):
    def __init__(
        self,
        id: SerialId,
        role_id: SerialId,
        permission_id: SerialId,
        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._role_id = role_id
        self._permission_id = permission_id

    @property
    def role_id(self) -> int:
        return self._role_id

    @async_property
    async def role(self):
        return await roleDao.get_by_id(self._role_id)

    @property
    def permission_id(self) -> int:
        return self._permission_id

    @async_property
    async def permission(self):
        return await permissionDao.get_by_id(self._permission_id)
