using Indotalent.Data.Entities;
using MediatR;
using Microsoft.AspNetCore.Identity;

namespace Indotalent.Features.Setting.SystemUser.Cqrs;


public record DeleteSystemUserByIdRequest(string Id);

public record DeleteSystemUserByIdCommand(DeleteSystemUserByIdRequest Data) : IRequest<bool>;

public class DeleteSystemUserByIdHandler : IRequestHandler<DeleteSystemUserByIdCommand, bool>
{
    private readonly UserManager<ApplicationUser> _userManager;

    public DeleteSystemUserByIdHandler(UserManager<ApplicationUser> userManager) => _userManager = userManager;

    public async Task<bool> Handle(DeleteSystemUserByIdCommand request, CancellationToken cancellationToken)
    {
        var user = await _userManager.FindByIdAsync(request.Data.Id);
        if (user == null) return false;

        var result = await _userManager.DeleteAsync(user);
        return result.Succeeded;
    }
}
