using System;
using System.Runtime.Serialization;

namespace KeycloakDemo
{
    /// <summary>
    /// Keycloak Token 响应模型
    /// </summary>
    [DataContract]
    public class TokenResponse
    {
        /// <summary>
        /// 访问令牌 - 用于调用 API
        /// </summary>
        [DataMember(Name = "access_token")]
        public string AccessToken { get; set; }

        /// <summary>
        /// 刷新令牌 - 用于刷新 access_token
        /// </summary>
        [DataMember(Name = "refresh_token")]
        public string RefreshToken { get; set; }

        /// <summary>
        /// 身份令牌 - 包含用户信息
        /// </summary>
        [DataMember(Name = "id_token")]
        public string IdToken { get; set; }

        /// <summary>
        /// Access Token 有效期（秒）
        /// </summary>
        [DataMember(Name = "expires_in")]
        public int ExpiresIn { get; set; }

        /// <summary>
        /// Refresh Token 有效期（秒）
        /// </summary>
        [DataMember(Name = "refresh_expires_in")]
        public int RefreshExpiresIn { get; set; }

        /// <summary>
        /// 令牌类型，固定为 Bearer
        /// </summary>
        [DataMember(Name = "token_type")]
        public string TokenType { get; set; }

        /// <summary>
        /// 会话状态 ID
        /// </summary>
        [DataMember(Name = "session_state")]
        public string SessionState { get; set; }

        /// <summary>
        /// 授权范围
        /// </summary>
        [DataMember(Name = "scope")]
        public string Scope { get; set; }

        /// <summary>
        /// 错误代码（请求失败时）
        /// </summary>
        [DataMember(Name = "error")]
        public string Error { get; set; }

        /// <summary>
        /// 错误描述（请求失败时）
        /// </summary>
        [DataMember(Name = "error_description")]
        public string ErrorDescription { get; set; }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool IsSuccess => string.IsNullOrEmpty(Error);

        /// <summary>
        /// 计算过期时间
        /// </summary>
        public DateTime GetExpiresAt()
        {
            return DateTime.UtcNow.AddSeconds(ExpiresIn);
        }
    }
}
