using Autofac;
using PlanTempus.Core.Configurations;
using PlanTempus.Core.Configurations.JsonConfigProvider;
using PlanTempus.Core.ModuleRegistry;
using PlanTempus.Core.SeqLogging;
using PlanTempus.Database.Core;

namespace PlanTempus.SetupInfrastructure
{
    public class Startup
    {
        public virtual IConfigurationRoot Configuration()
        {
            var configuration = new ConfigurationBuilder()
                .AddJsonFile("appconfiguration.json")
                .Build();

            return configuration;
        }

        public void ConfigureContainer(ContainerBuilder builder)
        {

            var configuration = Configuration();

            builder.RegisterModule(new Database.ModuleRegistry.DbPostgreSqlModule
            {
                ConnectionString = configuration.GetConnectionString("DefaultConnection")
            });

            builder.RegisterModule(new TelemetryModule
            {
                TelemetryConfig = configuration.GetSection("ApplicationInsights").ToObject<TelemetryConfig>()
            });

            builder.RegisterModule(new SeqLoggingModule
            {
                SeqConfiguration = configuration.GetSection("SeqConfiguration").ToObject<SeqConfiguration>()
            });


            builder.RegisterAssemblyTypes(typeof(IDbConfigure<>).Assembly)
                .AsClosedTypesOf(typeof(IDbConfigure<>))
                .AsSelf();

            builder.RegisterType<ConsoleService>();

        }
    }
}
