namespace CloudGaming.Code.DataAccess.MultiTenantUtil
{
    /// <summary>
    /// 多租户扩展 
    /// </summary>
    public static class MultiTenantExtension
    {
        /// <summary>
        /// 多租户 IServiceCollection 扩展 
        /// </summary>
        /// <param name="builder"></param>
        public static void AddDataBase(this IHostApplicationBuilder builder)
        {
            // 获取初始连接字符串
            string userConnectionString = builder.Configuration.GetConnectionString("UserConnectionString") ?? "";
            string gameConnectionString = builder.Configuration.GetConnectionString("GameConnectionString") ?? "";
            string phoneConnectionString = builder.Configuration.GetConnectionString("PhoneConnectionString") ?? "";
            string extConnectionString = builder.Configuration.GetConnectionString("ExtConnectionString") ?? "";

            // 注册 用户DbContext
            RegisterDbContext<CloudGamingUserContext>(builder, userConnectionString, AppDataBaseType.User);
            //注册 游戏Db
            RegisterDbContext<CloudGamingGameContext>(builder, gameConnectionString, AppDataBaseType.Game);
            //注册 app配置Db
            RegisterDbContext<CloudGamingPhoneContext>(builder, phoneConnectionString, AppDataBaseType.App);
            //注册 扩展Db
            RegisterDbContext<CloudGamingCBTContext>(builder, extConnectionString, AppDataBaseType.Ext);
        }

        /// <summary>
        /// 注册 DbContext，避免重复代码
        /// </summary>
        /// <typeparam name="TContext">要注册的 DbContext 类型</typeparam>
        /// <param name="builder">应用构建器</param>
        /// <param name="defaultConnectionString">默认连接字符串</param>
        /// <param name="configKey">AppConfig 中的连接字符串键</param>
        private static void RegisterDbContext<TContext>(IHostApplicationBuilder builder, string defaultConnectionString, AppDataBaseType configKey) where TContext : DbContext
        {
            builder.Services.AddDbContext<TContext>((serviceProvider, options) =>
            {
                var appConfig = serviceProvider.GetRequiredService<AppConfig>();
                string dbConnectionString = appConfig?.GetConnectionString(configKey) ?? defaultConnectionString;

                if (string.IsNullOrEmpty(dbConnectionString))
                {
                    dbConnectionString = defaultConnectionString;
                }

                options.UseSqlServer(dbConnectionString);
            }, ServiceLifetime.Scoped);
        }
        /// <summary>
        /// 多租户IApplicationBuilder扩展
        /// </summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseMultiTenant(this IApplicationBuilder app)
        {
            return app.UseMiddleware<MultiTenantTenantMiddleware>();
        }
        /// <summary>
        /// 多租户IApplicationBuilder扩展
        /// </summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseMultiTenantExt(this IApplicationBuilder app)
        {
            return app.UseMiddleware<MultiTenantExtMiddleware>();
        }
        

    }
}
