using Diligent.WebAPI.Business.Interfaces;
using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Host.Exceptions;
using Diligent.WebAPI.Host.Mediator.Request.Commands;
using MediatR;

namespace Diligent.WebAPI.Host.Mediator.Request.Handlers
{
    public class RejectCustomerRequestHandler : IRequestHandler<RejectCustomerRequestCommand, string?>
    {
        private readonly IRequestRepository _requestService;

        public RejectCustomerRequestHandler(IRequestRepository requestService)
        {
            _requestService = requestService;
        }

        public async Task<string?> Handle(RejectCustomerRequestCommand request, CancellationToken cancellationToken)
        {
            var req = await _requestService.FindRequestAsync(request.CustomerId, request.RoomId);

            if(req == null)
            {
                throw new NotFoundException("Request are not found");
            }

            await _requestService.RemoveAsync(req.Id);

            return req.Id;
        }
    }
}
