using Yuna.Website.Server.Infrastructure;
using Yuna.Website.Server.Model;
using Yuna.Website.Server.Storage;
using Yuna.Website.Server.Storage.Repositories.Device;

namespace Yuna.Tests.Repositories
{
    public class DeviceRepositoriesTests
    {
        [Fact]
        public async Task Create_Creates_Device()
        {
            // Arrange
            Settings.Init();
            var _context = new DapperContext(true);
            var repo = new DeviceRepository(_context);

            // Act
            Device device = new Device
            {
                Name = "Test Device",
                Description = "Test Description",
                DeviceUrl = "Test Url",
                UserId = 1
            };
            var result = await repo.Create(device);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(result.Name, device.Name);
            Assert.Equal(result.Description, device.Description);
            Assert.Equal(result.DeviceUrl, device.DeviceUrl);
            Assert.Equal(result.UserId, device.UserId);
        }

        [Fact]
        public async Task GetById_Gets_DeviceWithId()
        {
            // Arrange
            Settings.Init();
            var _context = new DapperContext(true);
            var repo = new DeviceRepository(_context);

            // Act
            var result = await repo.GetById(1);

            // Assert
            Assert.NotNull(result);
            Assert.Equal(1, result.Id);
        }

        [Fact]
        public async Task GetList_Gets_List()
        {
            // Arrange
            Settings.Init();
            var _context = new DapperContext(true);
            var repo = new DeviceRepository(_context);

            // Act
            var result = await repo.GetList();

            // Assert
            Assert.NotNull(result);
            Assert.NotEmpty(result);
        }

        [Fact]
        public async Task GetListForUser_GetsListForUser()
        {
            // Arrange
            Settings.Init();
            var _context = new DapperContext(true);
            var repo = new DeviceRepository(_context);

            //Act
            var result = await repo.GetList(1);

            //Assert
            Assert.NotNull(result);
            Assert.True(result.All(x => x.UserId == 1));
        }

        [Fact]
        public async Task Update_Updates()
        {
            //Arrange
            Settings.Init();
            var _context = new DapperContext(true);
            var repo = new DeviceRepository(_context);

            //Act
            var initial = await repo.GetById(1);
            
            initial!.Name += "1";
            var returnResult = await repo.Update(initial);

            var realChanged = await repo.GetById(1);


            //Assert
            Assert.Equal(initial.Name, returnResult!.Name);
            Assert.Equal(initial!.Name, realChanged!.Name);
        }
    }
}