from core.database.abc.db_model_dao_abc import DbModelDaoABC
from core.logger import DBLogger
from data.schemas.permission.api_key_permission import ApiKeyPermission

logger = DBLogger(__name__)


class ApiKeyPermissionDao(DbModelDaoABC[ApiKeyPermission]):

    def __init__(self):
        DbModelDaoABC.__init__(
            self, __name__, ApiKeyPermission, "permission.api_key_permissions"
        )

        self.attribute(ApiKeyPermission.api_key_id, int)
        self.attribute(ApiKeyPermission.permission_id, int)

    async def find_by_api_key_id(
        self, api_key_id: int, with_deleted=False
    ) -> list[ApiKeyPermission]:
        f = [{ApiKeyPermission.api_key_id: api_key_id}]
        if not with_deleted:
            f.append({ApiKeyPermission.deleted: False})

        return await self.find_by(f)

    async def find_by_permission_id(
        self, permission_id: int, with_deleted=False
    ) -> list[ApiKeyPermission]:
        f = [{ApiKeyPermission.permission_id: permission_id}]
        if not with_deleted:
            f.append({ApiKeyPermission.deleted: False})

        return await self.find_by(f)


apiKeyPermissionDao = ApiKeyPermissionDao()
