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 ConfigUnitsRepositoryTest
{
    /// <summary>
    /// Performs one-time setup for integration tests by resetting the <c>config_units</c> collection and seeding it with a single <see cref="ConfigUnits"/> document (Id "PV1") containing two empty <see cref="ConfigUnitItem"/> entries, then initializes the <see cref="ConfigUnitsRepository"/> used by the tests.
    /// </summary>
    [OneTimeSetUp]
        public async Task Init()
        {
            _optionsApiSettings = Options.Create(_apiSettings);
    
            var configUnitItem1 = new ConfigUnitItem();
            var configUnitItem2 = new ConfigUnitItem();
    
            var configUnits = new ConfigUnits
            {
                Id = "PV1",
                Items = [configUnitItem1, configUnitItem2]
            };
    
            await IntegrationDb.Database.DropCollectionAsync("config_units");
            await IntegrationDb.Database.CreateCollectionAsync("config_units");
    
            _repository = new ConfigUnitsRepository(_optionsApiSettings, IntegrationDb.Database);
    
            await _repository.InsertOneAsync(configUnits);
        }

    private ConfigUnitsRepository _repository;

    private readonly ApiSettings _apiSettings = new()
    {
        ConfigUnits = "config_units"
    };

    private IOptions<ApiSettings> _optionsApiSettings;


    /// <summary>
    /// Verifies that the repository's <c>FindById</c> method 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 <see cref="ConfigUnitsRepository.FindById"/> returns null when invoked with an identifier ("PV2") that does not exist in the repository.
    /// </summary>
    [Test]
        public async Task FindAsync_not_Find_id_Return_Empty_List()
        {
            var result = await _repository.FindById("PV2");
    
            Assert.That(result, Is.Null);
        }
}