using MediatR;
using Modules.User.Application.Gateways;

namespace Modules.User.Application.Commands.User.Access;

public class RevokeRoleFromUserCommand : IRequest<Unit>
{ 
    public Guid AccountId { get; set; }
    public int RoleId { get; set; }
    public string? RevokeReason { get; set; }
}

public class RevokeRoleFromUserCommandHandler(UserContext userContext, IUserRepository userRepository,
    IUnitOfWork unitOfWork) : IRequestHandler<RevokeRoleFromUserCommand, Unit>
{
    public async Task<Unit> Handle(RevokeRoleFromUserCommand request, CancellationToken cancellationToken)
    {
        var revokerId = userContext.GetAccountId();
        if (!revokerId.HasValue) return Unit.Value;
        //TODO: check privilegiess and ban status
        //TODO: if (user == null) throw new UserNotFoundException(); //"User not found"
        var user = await userRepository.GetByAccountIdAsync(request.AccountId, cancellationToken);
        if (user == null) return Unit.Value;
        //TODO: if (user == null) throw new UserNotFoundException(); //"User not found"
        
        user.Account.RevokeRole(request.RoleId, DateTime.UtcNow, revokerId.Value, request.RevokeReason);
        await userRepository.SaveAsync(user, cancellationToken);
        await unitOfWork.SaveChangesAsync(cancellationToken);
        return Unit.Value;
    }
}