using SIGCM2.Application.Abstractions;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Roles.Dtos;

namespace SIGCM2.Application.Roles.List;

public sealed class ListRolesQueryHandler : ICommandHandler<ListRolesQuery, IReadOnlyList<RolDto>>
{
    private readonly IRolRepository _repository;

    public ListRolesQueryHandler(IRolRepository repository)
    {
        _repository = repository;
    }

    public async Task<IReadOnlyList<RolDto>> Handle(ListRolesQuery query)
    {
        var roles = await _repository.ListAsync();
        return roles
            .Select(r => new RolDto(r.Id, r.Codigo, r.Nombre, r.Descripcion, r.Activo, r.FechaCreacion, r.FechaModificacion))
            .ToList();
    }
}
