using CrazyStudio.Core.Common.AliyunExt.Oss.Tools;
using CrazyStudio.Core.Common.Converters;
using CrazyStudio.Core.Common.Eitities;
using CrazyStudio.Core.Common.Tools.Extensions;
using SqlSugar;
using XPrintServer.Business.Extensions;
using XPrintServer.DataModel;
using XPrintServer.DataModel.Models;

var builder = WebApplication.CreateBuilder(args);
//添加配置文件信息
builder.Configuration.AddConfigureFiles();
// Add services to the container.

var sm2Infos = builder.Configuration.GetSection("SM2Infos").Get<SM2Infos>()!;
builder.Services.AddSingleton(sm2Infos);

builder.Services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

//添加 DistributedMemoryCache 服务
builder.Services.AddDistributedMemoryCache();
//添加 Session 服务
builder.Services.AddSession();

builder.Services.AddJwt(builder.Configuration);

var aliyunInfos = builder.Configuration.GetSection("AliyunInfos").Get<AliyunInfos>()!;
var ossTool = new OssTool(aliyunInfos.AccessKeyId, aliyunInfos.AccessKeySecret, aliyunInfos.Oss.Region + ".aliyuncs.com");
//注入阿里云相关配置
builder.Services.AddSingleton(aliyunInfos);
//注入阿里云OSS工具
builder.Services.AddSingleton(ossTool);

//SqlSugar ORM 配置
builder.Services.AddSqlSugar(builder.Configuration);
//注入仓储
builder.Services.AddScoped(typeof(SqlSugarRepository<>));
//注入各Service
builder.Services.AddServices(builder.Configuration);

//设置SnowIdInfo
//builder.Services.SetSnowIdInfo(builder.Configuration);
//获取数据库配置
builder.Services.GetSqlSugarConfig(builder.Configuration);
//同步数据库结构
builder.Services.DBSyncTables(builder.Configuration);


// 跨域配置
builder.Services.AddCors(builder.Configuration);

builder.Services.AddControllers().AddJsonOptions((options) =>
{
    //添加字符串到Guid的序列化处理，用来修复提交接收类型有Guid的情况下的错误
    options.JsonSerializerOptions.Converters.Add(new StringToGuidConverter());
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();


var app = builder.Build();

app.UseSession();

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

#region 身份验证
app.UseAuthentication();
app.UseAuthorization();
#endregion

app.MapControllers();

app.Run();
