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 UserRepositoryTest
{
    /// <summary>
    /// One-time integration test setup that prepares a clean "users" collection in the test database and seeds it with two predefined users, ensuring a deterministic state for downstream test cases.
    /// </summary>
    [OneTimeSetUp]
        public async Task Init()
        {
            _optionsApiSettings = Options.Create(_apiSettings);
    
            var user1 = new User
            {
                Id = ObjectId.GenerateNewId(),
                UserName = "username1",
                Password = "password1"
                //Rol = "rol1"
            };
    
            var user2 = new User
            {
                Id = ObjectId.GenerateNewId(),
                UserName = "username2",
                Password = "password2"
                //Rol = "rol2"
            };
    
            await IntegrationDb.Database.DropCollectionAsync("users");
            await IntegrationDb.Database.CreateCollectionAsync("users");
    
            _repository = new UserRepository(_optionsApiSettings, IntegrationDb.Database);
    
            await _repository.InsertOneAsync(user1);
            await _repository.InsertOneAsync(user2);
        }

    private UserRepository _repository = null!;

    private readonly ApiSettings _apiSettings = new()
    {
        Users = "users"
    };

    private IOptions<ApiSettings> _optionsApiSettings = null!;


    /// <summary>
    /// Verifies that the <see cref="_repository"/>'s <c>GetUser</c> method asynchronously returns a non-null user with the expected username and password credentials.
    /// </summary>
    [Test]
        public async Task GetUser()
        {
            var result = await _repository.GetUser("username1", "password1");
    
            Assert.That(result, Is.Not.Null);
            using (Assert.EnterMultipleScope())
            {
                Assert.That(result!.UserName, Is.EqualTo("username1"));
                Assert.That(result.Password, Is.EqualTo("password1"));
                //Assert.That(result.Rol, Is.EqualTo("rol1"));
            };
        }
}