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.administration.api_key_dao import apiKeyDao
from data.schemas.permission.permission_dao import permissionDao


class ApiKeyPermission(DbModelABC):
    def __init__(
        self,
        id: SerialId,
        api_key_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._api_key_id = api_key_id
        self._permission_id = permission_id

    @property
    def api_key_id(self) -> int:
        return self._api_key_id

    @async_property
    async def api_key(self):
        return await apiKeyDao.get_by_id(self._api_key_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)
