using Atomx.Admin.Client.Services;
using Microsoft.AspNetCore.Authorization;

namespace Atomx.Admin.Utils
{

    public class PermissionRequirement : IAuthorizationRequirement
    {
        public string Permission { get; }

        public PermissionRequirement(string permission)
        {
            Permission = permission;
        }
    }


    public class PermissionAuthorizationHandler : AuthorizationHandler<PermissionRequirement>
    {
        private readonly IPermissionService _permissionService;

        public PermissionAuthorizationHandler(IPermissionService permissionService)
        {
            _permissionService = permissionService;
        }

        protected override async Task HandleRequirementAsync(
            AuthorizationHandlerContext context,
            PermissionRequirement requirement)
        {
            if (await _permissionService.HasPermissionAsync(requirement.Permission))
            {
                context.Succeed(requirement);
            }
        }
    }
}