using Microsoft.Extensions.Configuration;


namespace Sln.Iot.Config
{
    public class AppConfigSetting
    {
        public static AppConfig? _config;

        /// <summary>
        /// 加载配置文件并解析为 AppConfig 对象。
        /// </summary>
        public static AppConfig Load()
        {
            if (_config != null)
                return _config; // 已经加载过就直接返回（模拟单例）

            var configBuilder = new ConfigurationBuilder()
                .SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

            IConfiguration configuration = configBuilder.Build();
            _config = configuration.GetSection("AppConfig").Get<AppConfig>();

            if (_config == null)
            {
                throw new InvalidOperationException("无法从 appsettings.json 加载 AppConfig 配置。");
            }

            return _config;
        }
    }
}
