using Autofac;
using e_suite.Database.Core;
using e_suite.Database.SqlServer;
using e_suite.DependencyInjection;
using e_suite.Scheduler.Jobs;
using eSuite.Core.Clock;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace e_suite.Scheduler.DependencyInjection;

/// <summary>
/// Used as a the primary location for IOC type registration for e-suite.
/// </summary>
public class CoreRegistrationModule : ESuiteModule
{
    /// <summary>
    /// Use the builder to register all the types and interfaces that the API requires to operate properly.
    /// </summary>
    /// <param name="builder"></param>
    protected override void Load(ContainerBuilder builder)
    {
        base.Load(builder);
        
        var loggerFactory = LoggerFactory.Create(loggingBuilder => loggingBuilder.AddConsole());
        builder.Register( x=> loggerFactory.CreateLogger("e-Suite.Scheduler")).As<ILogger>().SingleInstance();
        
        builder.RegisterInstance(ESuiteDatabaseExtension.BuildConfiguration()).As<IConfiguration>().SingleInstance();
        builder.Register(c =>
            {
                var clock = c.Resolve<IClock>();
                return ESuiteDatabaseExtension.CreateDatabase(clock)
                    .GetAwaiter()
                    .GetResult();
            })
            .As<IEsuiteDatabaseDbContext>()
            .InstancePerLifetimeScope();

        builder.RegisterType<NightlyCleanUp>();
        builder.RegisterType<NightlySigmaImport>();
    }
}