using System.Configuration;

namespace KeycloakDemo
{
    /// <summary>
    /// Keycloak 配置读取类
    /// </summary>
    public static class KeycloakConfig
    {
        /// <summary>
        /// Keycloak 服务器地址 (包含 realm)
        /// 例如: https://keycloak.company.com/realms/corp
        /// </summary>
        public static string Authority => 
            ConfigurationManager.AppSettings["Keycloak:Authority"] 
            ?? "https://keycloak.company.com/realms/corp";

        /// <summary>
        /// 客户端 ID
        /// </summary>
        public static string ClientId => 
            ConfigurationManager.AppSettings["Keycloak:ClientId"] 
            ?? "winform-legacy-app";

        /// <summary>
        /// 请求的权限范围
        /// </summary>
        public static string Scope => 
            ConfigurationManager.AppSettings["Keycloak:Scope"] 
            ?? "openid profile email";

        /// <summary>
        /// 本地回调监听端口
        /// </summary>
        public static int CallbackPort => 
            int.TryParse(ConfigurationManager.AppSettings["Keycloak:CallbackPort"], out var port) 
            ? port : 18080;

        /// <summary>
        /// 本地回调地址
        /// </summary>
        public static string RedirectUri => $"http://localhost:{CallbackPort}/callback";

        /// <summary>
        /// Token 刷新提前量 (秒)
        /// </summary>
        public static int TokenRefreshBuffer => 
            int.TryParse(ConfigurationManager.AppSettings["Keycloak:TokenRefreshBuffer"], out var buffer) 
            ? buffer : 60;

        /// <summary>
        /// 是否启用 Token 本地缓存
        /// </summary>
        public static bool EnableTokenCache => 
            bool.TryParse(ConfigurationManager.AppSettings["Keycloak:EnableTokenCache"], out var enable) 
            && enable;

        /// <summary>
        /// 授权端点
        /// </summary>
        public static string AuthorizationEndpoint => $"{Authority}/protocol/openid-connect/auth";

        /// <summary>
        /// Token 端点
        /// </summary>
        public static string TokenEndpoint => $"{Authority}/protocol/openid-connect/token";

        /// <summary>
        /// 登出端点
        /// </summary>
        public static string LogoutEndpoint => $"{Authority}/protocol/openid-connect/logout";

        /// <summary>
        /// 用户信息端点
        /// </summary>
        public static string UserInfoEndpoint => $"{Authority}/protocol/openid-connect/userinfo";
    }
}
