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 PoCMappingRepositoryTest
{
    /// <summary>
    /// Performs one-time setup for integration tests by seeding the "mappings" collection with a sample <see cref="PoCMapping"/> document, where a single point of care is mapped from an original value to a new one across eleven bed entries, and initializing the <see cref="PoCMappingRepository"/> against the integration database.
    /// </summary>
    [OneTimeSetUp]
    public async Task Init()
    {
        _optionsApiSettings = Options.Create(_apiSettings);

        var mappingItem1 = new PoCMappingItem
        {
            OriginalPoC = "original1",
            NewPoC = "new1",
            Beds =
            [
                new List<string> { "bed1", "bed 1" },
                    new List<string> { "bed2", "bed 2" },
                    new List<string> { "bed3", "bed 3" },
                    new List<string> { "bed4", "bed 4" },
                    new List<string> { "bed5", "bed 5" },
                    new List<string> { "bed6", "bed 6" },
                    new List<string> { "bed7", "bed 7" },
                    new List<string> { "bed8", "bed 8" },
                    new List<string> { "bed9", "bed 9" },
                    new List<string> { "bed10", "bed 10" },
                    new List<string> { "bed11", "bed 11" }
            ]
        };

        var pointOfCares = new List<PoCMappingItem> { mappingItem1 };

        var mapping = new PoCMapping
        {
            Id = "PV1",
            PointOfCares = pointOfCares
        };

        await IntegrationDb.Database.DropCollectionAsync("mappings");
        await IntegrationDb.Database.CreateCollectionAsync("mappings");

        _repository = new PoCMappingRepository(_optionsApiSettings, IntegrationDb.Database);

        await _repository.InsertOneAsync(mapping);
    }

    private PoCMappingRepository _repository;

    private readonly ApiSettings _apiSettings = new();

    private IOptions<ApiSettings> _optionsApiSettings;


    /// <summary>
    /// Verifies that finding a mapping by the key "PV1" returns a non-null result containing point of care entries with at least one associated bed.
    /// </summary>
    [Test]
    public async Task FindByKey_Find_Return_Mapping()
    {
        var result = await _repository.FindByKey("PV1");

        Assert.That(result, Is.Not.Null);
        Assert.That(result!.PointOfCares, Has.Count.GreaterThan(0));
        Assert.That(result.PointOfCares[0].Beds, Has.Count.GreaterThan(0));
    }

    /// <summary>
    /// Verifies that the repository's FindByKey method returns null when the provided key does not correspond to an existing entity, confirming the not-found behavior.
    /// </summary>
    [Test]
    public async Task FindByKey_Not_Find_Return_Mapping()
    {
        var result = await _repository.FindByKey("PV2");

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