using NetinaCMS.Api.WebFramework.Configurations;
using NetinaCMS.Api.WebFramework.MiddleWares;
using NetinaCMS.Api.WebFramework.Swagger;

var builder = WebApplication.CreateBuilder(args);
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
builder.Host.UseSerilog();
LoggerConfig.ConfigureSerilog();
string env = builder.Environment.IsDevelopment() == true ? "Development" : "Production";
builder.Host.UseContentRoot(Directory.GetCurrentDirectory());
if (builder.Environment.IsDevelopment())
    builder.Configuration
        .AddJsonFile($"AppSettings/appsettings.json")
        .AddJsonFile($"AppSettings/appsettings.{env}.json");

if (builder.Environment.IsProduction())
    builder.Configuration.AddJsonFile($"AppSettings/Production/appsettings.{env}.json");
var configuration = builder.Configuration;
var siteSetting = configuration.GetSection(nameof(SiteSettings)).Get<SiteSettings>();
builder.Services.Configure<SiteSettings>(configuration.GetSection(nameof(SiteSettings)));



// Add services to the container.

builder.Services.AddControllers();
builder.Services.AddCustomCores();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddCustomSwagger(siteSetting!.BaseUrl);
builder.Services.AddCustomApiVersioning();
builder.Services.AddCustomController();
builder.Services.AddControllers();
builder.Services.AddCustomResponseCompression();
builder.Services.AddValidatorsFromAssembly(typeof(RepositoryConfig).Assembly, includeInternalTypes: true);
builder.Services.AddCustomMvc();
builder.Services.AddCustomAuthorization();
builder.Services.AddJwtCustomAuthentication(siteSetting.JwtSettings);
builder.Services.AddMvcCore().AddRazorPages().AddRazorViewEngine().AddViews();
builder.Services.AddCustomIdentity();
builder.Services.AddCustomDbContext(configuration);
builder.Services.AddMarten(configuration, builder.Environment);
builder.Services.AddCarter();


builder.Host.ConfigureContainer<ContainerBuilder>(builder =>
{

    var assembly = typeof(CoreConfig).Assembly;
    builder
        .RegisterAssemblyTypes(assembly)
        .AssignableTo<IScopedDependency>()
        .AsImplementedInterfaces()
        .InstancePerLifetimeScope();

    var assemblyB = typeof(InfrastructureConfig).Assembly;
    builder.RegisterAssemblyTypes(assemblyB)
        .AssignableTo<IScopedDependency>()
        .AsImplementedInterfaces()
        .InstancePerLifetimeScope();

    var assemblyC = typeof(RepositoryConfig).Assembly;
    builder.RegisterAssemblyTypes(assemblyC)
        .AssignableTo<IScopedDependency>()
        .AsImplementedInterfaces()
        .InstancePerLifetimeScope();


    var assemblyD = typeof(Program).Assembly;
    builder.RegisterAssemblyTypes(assemblyD)
        .AssignableTo<IScopedDependency>()
        .AsImplementedInterfaces()
        .InstancePerLifetimeScope();

    builder.RegisterMediatR(MediatRConfigurationBuilder
        .Create(typeof(RepositoryConfig).Assembly)
        .WithCustomPipelineBehavior(typeof(ValidationBehavior<,>))
        .WithAllOpenGenericHandlerTypesRegistered()
        .Build());

    builder.RegisterMediatR(MediatRConfigurationBuilder
        .Create(typeof(CoreConfig).Assembly)
        .WithCustomPipelineBehavior(typeof(ValidationBehavior<,>))
        .WithAllOpenGenericHandlerTypesRegistered()
        .Build());

    builder.RegisterMediatR(MediatRConfigurationBuilder
        .Create(typeof(DomainConfig).Assembly)
        .WithCustomPipelineBehavior(typeof(ValidationBehavior<,>))
        .WithAllOpenGenericHandlerTypesRegistered()
        .Build());
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    //app.UseSwagger();
    //app.UseSwaggerUI();
}
app.UseCors("CorsPolicy");

app.UseCustomSwagger(siteSetting.BaseUrl);

app.UseAuthorization();
app.UseAuthentication();

app.UseExceptionHandlerMiddleware();

app.MapCarter();
app.UseStaticFiles();
await app.InitialDb();
app.MapControllers();

app.Run();