using Autofac;
using eSuite.Core.Clock;

namespace e_suite.DependencyInjection;

public class ESuiteModule : Autofac.Module
{
    private readonly IModuleDiscovery _moduleDiscovery;

    public ESuiteModule()
        : this(CreateDefaultDiscovery())
    {
    }

    public ESuiteModule(IModuleDiscovery moduleDiscovery)
    {
        _moduleDiscovery = moduleDiscovery;
    }

    private static IModuleDiscovery CreateDefaultDiscovery() =>
        new ModuleDiscovery(
            new RealFileSystem(),
            new DefaultAssemblyLoader(),
            new EntryAssemblyIdentity()
        );

    protected override void Load(ContainerBuilder builder)
    {
        base.Load(builder);
        builder.RegisterType<UtcClock>().As<IClock>().SingleInstance();

        _moduleDiscovery.RegisterAllModuleIocRegistrations(builder);
    }
}