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
{
    [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;


    [Test]
    public async Task FindById_Find_id_Return_List()
    {
        var result = await _repository.FindById("PV1");

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

    [Test]
    public async Task FindAsync_not_Find_id_Return_Empty_List()
    {
        var result = await _repository.FindById("PV2");

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

    [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);
    }
}