using JiShe.CollectBusEPO.Extensions.HealthCheck;
using JiShe.CollectBusEPO.HttpApi.Host.Swaggers;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using Volo.Abp.Swashbuckle;

namespace JiShe.CollectBusEPO;

[DependsOn(
    typeof(CollectBusEPOHttpApiModule),
    typeof(AbpAspNetCoreAuthenticationJwtBearerModule),
    typeof(AbpAspNetCoreSerilogModule),
    typeof(AbpSwashbuckleModule),
    typeof(CollectBusEPOApplicationModule)
)]
public partial class CollectBusEPOHttpApiHostModule : AbpModule
{
    public override void OnPostApplicationInitialization(ApplicationInitializationContext context)
    { 
        base.OnPostApplicationInitialization(context);
    }

    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        var configuration = context.Services.GetConfiguration();
        ConfigureSerilog(context);

        ConfigureCache(context);
        ConfigureSwaggerServices(context, configuration);
        ConfigureJwtAuthentication(context, configuration);
        ConfigureIdentity(context);
        ConfigureAuditLog(context);
        ConfigureCustom(context, configuration);
      
    }

    public override void OnApplicationInitialization(ApplicationInitializationContext context)
    {
        var app = context.GetApplicationBuilder();
        var configuration = context.GetConfiguration();
        app.UseCorrelationId();
        app.UseStaticFiles();


        app.UseRouting();
        app.UseCors(CollectBusEPOHttpApiHostConst.DefaultCorsPolicyName);
          
     
        app.UseAuthorization();
        app.UseSwagger();
        app.UseAbpSwaggerUI(options =>
        {
            configuration.GetSection("SwaggerConfig").Get<List<SwaggerConfig>>().ForEach(group =>
            {
                options.SwaggerEndpoint($"/swagger/{group.GroupName}/swagger.json", group.Title); //分组显示
            });
            options.DocExpansion(DocExpansion.None);
            options.DefaultModelsExpandDepth(-1);
        });

        app.UseAuditing();
        app.UseAbpSerilogEnrichers();
        app.UseUnitOfWork();
        app.UseConfiguredEndpoints(endpoints =>
        {
            //endpoints.MapHealthChecks("/health", new HealthCheckOptions
            //{
            //    Predicate = _ => true,
            //    ResponseWriter = HealthCheckResponse.Writer
            //});
        });
      
    }
}