using Diligent.WebAPI.Business.Services;
using Diligent.WebAPI.Host.Mediator.Rooms.Commands;
using MediatR;

namespace Diligent.WebAPI.Host.Mediator.Chat.Handlers
{
    public class RemoveUserFromGroupHandler : IRequestHandler<RemoveUserFromGroupCommand, Unit>
    {
        private readonly RoomService _roomService;

        public RemoveUserFromGroupHandler(RoomService roomService)
        {
            _roomService = roomService;
        }

        public async Task<Unit> Handle(RemoveUserFromGroupCommand request, CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new BadHttpRequestException("Object cannot be null");
            }

            var result = await _roomService.LeaveChatRoom(request.RoomId, request.UserId);

            if (!result)
            {
                throw new Exception("Problem with deleting user from group");
            }

            return new Unit();
        }
    }
}
