from api_graphql.abc.mutation_abc import MutationABC
from api_graphql.input.role_create_input import RoleCreateInput
from api_graphql.input.role_update_input import RoleUpdateInput
from core.logger import APILogger
from data.schemas.permission.role import Role
from data.schemas.permission.role_dao import roleDao
from data.schemas.permission.role_permission import RolePermission
from data.schemas.permission.role_permission_dao import rolePermissionDao
from service.permission.permissions_enum import Permissions

logger = APILogger(__name__)


class RoleMutation(MutationABC):
    def __init__(self):
        MutationABC.__init__(self, "Role")
        self.mutation(
            "create",
            self.resolve_create,
            RoleCreateInput,
            require_any_permission=[Permissions.roles_create],
        )
        self.mutation(
            "update",
            self.resolve_update,
            RoleUpdateInput,
            require_any_permission=[Permissions.roles_update],
        )
        self.mutation(
            "delete",
            self.resolve_delete,
            require_any_permission=[Permissions.roles_delete],
        )
        self.mutation(
            "restore",
            self.resolve_restore,
            require_any_permission=[Permissions.roles_delete],
        )

    @staticmethod
    async def resolve_create(obj: RoleCreateInput, *_):
        logger.debug(f"create role: {obj.__dict__}")

        role = Role(
            0,
            obj.name,
            obj.description,
        )
        await roleDao.create(role)
        role = await roleDao.get_by_name(role.name)
        await rolePermissionDao.create_many(
            [RolePermission(0, role.id, x) for x in obj.permissions]
        )

        return role

    @staticmethod
    async def resolve_update(obj: RoleUpdateInput, *_):
        logger.debug(f"update role: {obj.__dict__}")
        role = await roleDao.get_by_id(obj.id)
        role.name = obj.get("name", role.name)
        role.description = obj.get("description", role.description)
        await roleDao.update(role)

        if obj.permissions is not None:
            permissions = [x for x in await rolePermissionDao.get_by_role_id(role.id)]

            to_delete = (
                permissions
                if len(obj.permissions) == 0
                else await rolePermissionDao.find_by(
                    [
                        {RolePermission.role_id: role.id},
                        {
                            RolePermission.permission_id: {
                                "notIn": obj.get("permissions", [])
                            }
                        },
                    ]
                )
            )
            permission_ids = [x.permission_id for x in permissions]
            deleted_permission_ids = [
                x.permission_id
                for x in await rolePermissionDao.find_by(
                    [{RolePermission.role_id: role.id}, {RolePermission.deleted: True}]
                )
            ]

            to_create = [
                RolePermission(0, role.id, x)
                for x in obj.permissions
                if x not in permission_ids and x not in deleted_permission_ids
            ]
            to_restore = [
                await rolePermissionDao.get_single_by(
                    [
                        {RolePermission.role_id: role.id},
                        {RolePermission.permission_id: x},
                    ]
                )
                for x in obj.permissions
                if x not in permission_ids and x in deleted_permission_ids
            ]

            if len(to_delete) > 0:
                await rolePermissionDao.delete_many(to_delete)

            if len(to_create) > 0:
                await rolePermissionDao.create_many(to_create)

            if len(to_restore) > 0:
                await rolePermissionDao.restore_many(to_restore)

        return role

    @staticmethod
    async def resolve_delete(*_, id: int):
        logger.debug(f"delete role: {id}")
        role = await roleDao.get_by_id(id)
        await roleDao.delete(role)
        return True

    @staticmethod
    async def resolve_restore(*_, id: int):
        logger.debug(f"restore role: {id}")
        role = await roleDao.get_by_id(id)
        await roleDao.restore(role)
        return True
