from functools import wraps 
from app.services.user import UserService
from flask_jwt_extended import jwt_required, get_jwt_identity

def admin_required():
    """
    decorator that check admin permissions and action
    """
    def decorator(f):
        @wraps(f)
        def wrapper(*args, **kwargs):
            auth_header = request.headers.get('Authorization')

            # Ensure the jwt-token is passed with the headers
            if auth_header:
                current_user = get_jwt_identity()
                user_model = UserService.get_user_by_email(current_user)
                print(user_model.is_admin)
                if user_model.is_admin:

                    return user_model
                else:
                    return {"error":"Permission Deneid","message": "You do not have permission to perform this action.!"}, 401
            else:
                return {"message": "No Authorization header found!"}, 401

        return wrapper

    return decorator
