using System.Diagnostics;
using adas_core.Domain.Models.AppSettings;
using adas_core.Domain.Models.MongoModels;
using adas_core.Infrastructure.Repositories;
using Microsoft.Extensions.Options;
using Options = Microsoft.Extensions.Options.Options;

namespace adas_core.Test.Repositories;

[TestFixture]
[Category("Integration")]
public class ConfigPumpsRepositoryTest
{
    /// <summary>
    /// Initializes the integration test environment by recreating the config_pumps collection and inserting a single configuration pump containing two empty items for use by subsequent tests.
    /// </summary>
    [OneTimeSetUp]
        public async Task Init()
        {
            _optionsApiSettings = Options.Create(_apiSettings);
    
            var configPumpItem1 = new ConfigPumpItem();
            var configPumpItem2 = new ConfigPumpItem();
    
            var configPumps = new ConfigPumps
            {
                Id = "PV1",
                Items = [configPumpItem1, configPumpItem2]
            };
    
            await IntegrationDb.Database.DropCollectionAsync("config_pumps");
            await IntegrationDb.Database.CreateCollectionAsync("config_pumps");
    
            _repository = new ConfigPumpsRepository(_optionsApiSettings, IntegrationDb.Database);
    
            await _repository.InsertOneAsync(configPumps);
        }

    private ConfigPumpsRepository _repository;

    private readonly ApiSettings _apiSettings = new()
    {
        ConfigPumps = "config_pumps"
    };

    private IOptions<ApiSettings> _optionsApiSettings;


    /// <summary>
    /// Verifies that <see cref="ConfigPumpsRepository.FindById"/> returns a non-null result when searching by the identifier "PV1".
    /// </summary>
    [Test]
        public async Task FindById_Find_id_Return_List()
        {
            var result = await _repository.FindById("PV1");
    
            Assert.That(result, Is.Not.Null);
        }

    /// <summary>
    /// Verifies that the repository's FindById method returns null when no entity is found for the specified identifier.
    /// </summary>
    [Test]
        public async Task FindAsync_not_Find_id_Return_Empty_List()
        {
            var result = await _repository.FindById("PV2");
    
            Assert.That(result, Is.Null);
        }

    /// <summary>
    /// Tests that inserting a new pump configuration with two items increases the total configuration count to two, that the inserted configuration can be retrieved by its identifier, and that the inserted configuration can be successfully deleted.
    /// </summary>
    [Test]
        public async Task Insert_PumpConfig_returns2()
        {
            var configPumpItem1 = new ConfigPumpItem();
            var configPumpItem2 = new ConfigPumpItem();
    
            var newPumpConfig = new ConfigPumps
            {
                Id = "PV2",
                Items = [configPumpItem1, configPumpItem2]
            };
    
            await _repository.InsertOneAsync(newPumpConfig);
    
            var result = await _repository.GetAllConfigs();
    
            Assert.That(result, Has.Count.EqualTo(2));
    
            var pumpCfgInserted = await _repository.FindById(newPumpConfig.Id);
    
            Assert.That(pumpCfgInserted, Is.Not.EqualTo(null));
    
            Debug.Assert(pumpCfgInserted != null, nameof(pumpCfgInserted) + " != null");
    
            await _repository.DeleteConfig(pumpCfgInserted);
        }
}