using AutoMapper;
using Diligent.WebAPI.Business.Interfaces;
using Diligent.WebAPI.Host.DTOs.Notification;
using Diligent.WebAPI.Host.Mediator.Notifications.Queries;
using MediatR;

namespace Diligent.WebAPI.Host.Mediator.Notifications.Handlers
{
    public class GetNotificationsHandler : IRequestHandler<GetNotificationsQuery, List<NotificationReadDTO>>
    {
        private readonly ICustomerService _customerService;
        private readonly IMapper _mapper;

        public GetNotificationsHandler(ICustomerService customerService, IMapper mapper)
        {
            _customerService = customerService;
            _mapper = mapper;
        }

        public async Task<List<NotificationReadDTO>> Handle(GetNotificationsQuery request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new BadHttpRequestException("User id cannot be null");
            }

            return _mapper.Map<List<NotificationReadDTO>>(await _customerService.ReadNotifications(request.UserId));
        }
    }
}
