using AutoMapper;
using Diligent.WebAPI.Business.Interfaces;
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Host.DTOs.Customer;
using Diligent.WebAPI.Host.Mediator.Request.Queries;
using MediatR;

namespace Diligent.WebAPI.Host.Mediator.Request.Handlers
{
    public class GetSendersForSpecificRoomHandler : IRequestHandler<GetSendersForSpecificRequestRoomQuery, List<CustomerRequestRoomReadDTO>>
    {
        private readonly IRequestRepository _requestService;
        private readonly IMapper _mapper;

        public GetSendersForSpecificRoomHandler(IRequestRepository requestService,IMapper mapper)
        {
            _requestService = requestService;
            _mapper = mapper;
        }
        public async Task<List<CustomerRequestRoomReadDTO>> Handle(GetSendersForSpecificRequestRoomQuery request, CancellationToken cancellationToken)
        {
            var requests = await _requestService.GetAsync();
            List<CustomerRequestRoomReadDTO> users = new();
            foreach (var req in requests)
            {
                if(req.RoomId == request.RoomId)
                {
                    users.Add(_mapper.Map<CustomerRequestRoomReadDTO>(req));
                }
            }
            return users;
        }
    }
}
