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

namespace Diligent.WebAPI.Host.Mediator.Request.Handlers
{
    public class GetRoomsForWhichRequestExistHandler : IRequestHandler<GetRoomsForWhichRequestExistQuery, List<RequestRoomReadDTO>>
    {
        private readonly IRequestRepository _requestService;
        private readonly RoomService _roomService;
        private readonly IMapper _mapper;

        public GetRoomsForWhichRequestExistHandler(IRequestRepository requestService,RoomService roomService,IMapper mapper)
        {
            _requestService = requestService;
            _roomService = roomService;
            _mapper = mapper;
        }
        public async Task<List<RequestRoomReadDTO>> Handle(GetRoomsForWhichRequestExistQuery request, CancellationToken cancellationToken)
        {
            var requests = await _requestService.GetAsync();
            var rooms = await _roomService.GetRoomsAsync();
            List<Room> temp = new();
            foreach (var req in requests)
            {
                foreach (var room in rooms)
                {
                    if (req.RoomId == room.Id)
                    {
                        if (!temp.Contains(room))
                        {
                            temp.Add(room);
                        }
                    }

                }
            }
            return _mapper.Map<List<RequestRoomReadDTO>>(temp);
        }
    }
}
