using Autofac;
using eSuite.Core.Clock;
using Moq;
using NUnit.Framework;

namespace e_suite.DependencyInjection.UnitTests;

[TestFixture]
public class ESuiteModuleUnitTests
{
    [Test]
    public void Load_CallsModuleDiscovery()
    {
        // Arrange
        var mockDiscovery = new Mock<IModuleDiscovery>();
        var module = new ESuiteModule(mockDiscovery.Object);

        ContainerBuilder? capturedBuilder = null;

        mockDiscovery
            .Setup(d => d.RegisterAllModuleIocRegistrations(It.IsAny<ContainerBuilder>()))
            .Callback<ContainerBuilder>(b => capturedBuilder = b);

        var builder = new ContainerBuilder();
        builder.RegisterModule(module);

        // Act
        builder.Build();

        // Assert
        mockDiscovery.Verify(
            d => d.RegisterAllModuleIocRegistrations(It.IsAny<ContainerBuilder>()),
            Times.Once);

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

    [Test]
    public void Load_RegistersUtcClockAsIClock()
    {
        // Arrange
        var mockDiscovery = new Mock<IModuleDiscovery>();
        // Don’t let discovery do anything in this test
        mockDiscovery
            .Setup(d => d.RegisterAllModuleIocRegistrations(It.IsAny<ContainerBuilder>()));

        var module = new ESuiteModule(mockDiscovery.Object);

        var builder = new ContainerBuilder();
        builder.RegisterModule(module);

        // Act
        var container = builder.Build();
        var clock = container.Resolve<IClock>();

        // Assert
        Assert.That(clock, Is.InstanceOf<UtcClock>());
    }
}