using AutoMapper;
using Diligent.WebAPI.Business.Interfaces;
using Diligent.WebAPI.Data.Entities;
using Diligent.WebAPI.Host.DTOs.Request;
using Diligent.WebAPI.Host.Mapper;
using Diligent.WebAPI.Host.Mediator.Request.Commands;
using Diligent.WebAPI.Host.Mediator.Request.Handlers;
using Microsoft.AspNetCore.Http;
using Moq;

namespace Tests
{
    [TestFixture]
    public class RequestTests
    {
        private Mock<IRequestRepository> _requestRepositoryMock;
        private CreateRequestHandler _handler;
        private IMapper _mapper;

        [SetUp]
        public void Setup()
        {
            _requestRepositoryMock = new Mock<IRequestRepository>();
            var myProfile = new RequestMappingProfile();
            var configuration = new MapperConfiguration(cfg => cfg.AddProfile(myProfile));
            _mapper = new Mapper(configuration);
            _handler = new CreateRequestHandler(_mapper, _requestRepositoryMock.Object);
        }

        [Test]
        public async Task CreateRequest_ObjectIsNull_ThrowsBadHttpRequestException()
        {
            var command = new CreateRequestCommand(null);

            Assert.That(async () => await _handler.Handle(command, new CancellationToken()), Throws.Exception.TypeOf<BadHttpRequestException>());
        }

        [Test]
        public async Task CreateRequest_ObjectIsNotNull_ReturnsObject()
        {
            _requestRepositoryMock.Setup(x => x.CreateAsync(It.IsAny<Request>())).Returns(Task.CompletedTask);
            var requestDTO = new RequestCreateDTO
            {
                RoomId = "room1",
                RoomName = "room1",
                SenderId = "sender1",
                SenderUsername = "sender1"
            };
            var command = new CreateRequestCommand(requestDTO);

            var result = await _handler.Handle(command, new CancellationToken());

            Assert.That(result, Is.Not.Null);
        }
    }
}