using adas_core.Domain.Models.AppSettings;
using adas_core.Domain.Models.MongoModels;
using adas_core.Infrastructure.Repositories;
using Microsoft.Extensions.Options;
using MongoDB.Bson;
using Options = Microsoft.Extensions.Options.Options;

namespace adas_core.Test.Repositories;

[TestFixture]
[Category("Integration")]
public class ServiceConfigRepositoryTest
{
    /// <summary>
    /// Performs one-time initialization for integration tests by creating a <see cref="ServiceConfig"/> entry in the test database and preparing the repository used by the test fixture.
    /// </summary>
    [OneTimeSetUp]
        public async Task Init()
        {
            _optionsApiSettings = Options.Create(_apiSettings);
    
            var serviceConfig = new ServiceConfig
            {
                Id = _id,
                StrId = Id.ToString()
            };
    
            await IntegrationDb.Database.DropCollectionAsync("service_config");
            await IntegrationDb.Database.CreateCollectionAsync("service_config");
    
            _repository = new ServiceConfigRepository(_optionsApiSettings, IntegrationDb.Database);
    
            await _repository.InsertOneAsync(serviceConfig);
        }

    private ServiceConfigRepository _repository;

    private readonly ApiSettings _apiSettings = new()
    {
        ServiceConfig = "service_config"
    };

    private IOptions<ApiSettings> _optionsApiSettings;

    private readonly ObjectId _id = ObjectId.GenerateNewId();
    private static readonly ObjectId Id = ObjectId.GenerateNewId();


    /// <summary>
    /// Verifies that <see cref="_repository"/>.FindById returns a non-null entity when queried by its string identifier,
    /// and that the returned entity's <c>StrId</c> matches the supplied id.
    /// </summary>
    [Test]
        public async Task FindById_Find_string()
        {
            var result = await _repository.FindById(Id.ToString());
    
            Assert.That(result, Is.Not.Null);
            Assert.That(result!.StrId, Is.EqualTo(Id.ToString()));
        }

    /// <summary>
    /// Verifies that the repository's FindById method successfully retrieves an object by its identifier,
    /// returning a non-null result whose Id matches the requested identifier.
    /// </summary>
    [Test]
        public async Task FindById_Find_ObjectId()
        {
            var result = await _repository.FindById(_id);
    
            Assert.That(result, Is.Not.Null);
            Assert.That(result!.Id, Is.EqualTo(_id));
        }
}