from core.database.abc.db_model_dao_abc import DbModelDaoABC
from core.logger import DBLogger
from data.schemas.permission.role_permission import RolePermission

logger = DBLogger(__name__)


class RolePermissionDao(DbModelDaoABC[RolePermission]):

    def __init__(self):
        DbModelDaoABC.__init__(
            self, __name__, RolePermission, "permission.role_permissions"
        )

        self.attribute(RolePermission.role_id, int)
        self.attribute(RolePermission.permission_id, int)

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

        return await self.find_by(f)

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

        return await self.find_by(f)


rolePermissionDao = RolePermissionDao()
