using QYZH.InteractiveMagazine.Models.Enum;
using Newtonsoft.Json;

namespace QYZH.InteractiveMagazine.Models.WeChat;

/// <summary>
/// 微信小程序初次登录输入
/// </summary>
public class WeChatLoginInput
{
    /// <summary>
    /// 微信登录凭证（wx.login 获取的 code）
    /// </summary>
    public string Code { get; set; } = string.Empty;

    /// <summary>
    /// 手机号获取凭证（getPhoneNumber 按钮回调中的 code，可选）
    /// </summary>
    public string? PhoneCode { get; set; }
}

/// <summary>
/// 微信小程序快捷登录输入（通过 OpenId 登录）
/// </summary>
public class WeChatQuickLoginInput
{
    /// <summary>
    /// 微信OpenId（初次登录后客户端缓存的 OpenId）
    /// </summary>
    public string OpenId { get; set; } = string.Empty;
}

/// <summary>
/// 微信登录输出（含微信用户信息 + 该微信下的 Users 列表）
/// </summary>
public class WeChatLoginOutput
{
    /// <summary>
    /// 访问令牌（同时携带 WxUserId 和当前激活 UserId）
    /// </summary>
    public string Token { get; set; } = string.Empty;

    /// <summary>
    /// 当前激活用户ID（Users.Id，无用户时为 0）
    /// </summary>
    public long CurrentUserId { get; set; }

    /// <summary>
    /// 微信用户信息
    /// </summary>
    public WxUserInfoOutput WxUser { get; set; } = new();

    /// <summary>
    /// 该微信用户下的用户列表
    /// </summary>
    public List<WxUserOutput> Users { get; set; } = [];
}

/// <summary>
/// 微信用户信息输出（WxUser 表数据）
/// </summary>
public class WxUserInfoOutput
{
    /// <summary>
    /// WxUser 主键Id
    /// </summary>
    public long Id { get; set; }

    /// <summary>
    /// 微信OpenId
    /// </summary>
    public string OpenId { get; set; } = string.Empty;

    /// <summary>
    /// 微信UnionId
    /// </summary>
    public string? UnionId { get; set; }

    /// <summary>
    /// 昵称
    /// </summary>
    public string? Name { get; set; }

    /// <summary>
    /// 头像地址
    /// </summary>
    public string? AvatarUrl { get; set; }

    /// <summary>
    /// 手机号
    /// </summary>
    public string? Phone { get; set; }
}

/// <summary>
/// 用户输出（Users 表数据，即角色/子用户）
/// </summary>
public class WxUserOutput
{
    /// <summary>
    /// Users 主键Id
    /// </summary>
    public long Id { get; set; }

    /// <summary>
    /// 关联 WxUserId
    /// </summary>
    public long WxUserId { get; set; }

    /// <summary>
    /// 昵称
    /// </summary>
    public string? NickName { get; set; }

    /// <summary>
    /// 头像地址
    /// </summary>
    public string? AvatarUrl { get; set; }

    /// <summary>
    /// 积分余额
    /// </summary>
    public int Points { get; set; }

    /// <summary>
    /// 当前成长值
    /// </summary>
    public int GrowthPoints { get; set; }

    /// <summary>
    /// 用户类型: Normal, VIP
    /// </summary>
    public string Type { get; set; } = string.Empty;

    /// <summary>
    /// 状态
    /// </summary>
    public string Status { get; set; } = string.Empty;

    /// <summary>
    /// 是否上次在线用户
    /// </summary>
    public bool IsLastOnline { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; }
}

/// <summary>
/// 新增用户输入（在 WxUser 下创建子用户/角色）
/// </summary>
public class CreateChildUserInput
{
    /// <summary>
    /// 昵称
    /// </summary>
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 头像地址（可选）
    /// </summary>
    public string? AvatarUrl { get; set; }
}

/// <summary>
/// 微信切换用户输入
/// </summary>
public class WeChatSwitchUserInput
{
    /// <summary>
    /// 目标用户ID（Users 表 Id）
    /// </summary>
    public long UserId { get; set; }
}

/// <summary>
/// 修改家长名字输入
/// </summary>
public class UpdateWxUserNameInput
{
    /// <summary>
    /// 新名字
    /// </summary>
    public string Name { get; set; } = string.Empty;
}

/// <summary>
/// 微信切换用户输出（重新生成 Token，切换激活用户）
/// </summary>
public class WeChatSwitchUserOutput
{
    /// <summary>
    /// 新的访问令牌（指向切换后的用户）
    /// </summary>
    public string Token { get; set; } = string.Empty;

    /// <summary>
    /// 当前切换的用户详情
    /// </summary>
    public WxUserOutput User { get; set; } = new();
}

// ========== 微信 API 响应 DTO（不变） ==========

/// <summary>
/// 微信 code2session 接口响应
/// </summary>
public class WxCode2SessionResponse
{
    [JsonProperty("openid")]
    public string? OpenId { get; set; }

    [JsonProperty("session_key")]
    public string? SessionKey { get; set; }

    [JsonProperty("unionid")]
    public string? UnionId { get; set; }

    [JsonProperty("errcode")]
    public int ErrCode { get; set; }

    [JsonProperty("errmsg")]
    public string? ErrMsg { get; set; }
}

/// <summary>
/// 微信获取手机号接口响应
/// </summary>
public class WxPhoneNumberResponse
{
    [JsonProperty("errcode")]
    public int ErrCode { get; set; }

    [JsonProperty("errmsg")]
    public string? ErrMsg { get; set; }

    [JsonProperty("phone_info")]
    public WxPhoneInfo? PhoneInfo { get; set; }
}

/// <summary>
/// 微信手机号信息
/// </summary>
public class WxPhoneInfo
{
    [JsonProperty("phoneNumber")]
    public string? PhoneNumber { get; set; }

    [JsonProperty("purePhoneNumber")]
    public string? PurePhoneNumber { get; set; }

    [JsonProperty("countryCode")]
    public string? CountryCode { get; set; }
}

/// <summary>
/// 微信 access_token 接口响应
/// </summary>
public class WxAccessTokenResponse
{
    [JsonProperty("access_token")]
    public string? AccessToken { get; set; }

    [JsonProperty("expires_in")]
    public int ExpiresIn { get; set; }

    [JsonProperty("errcode")]
    public int ErrCode { get; set; }

    [JsonProperty("errmsg")]
    public string? ErrMsg { get; set; }
}
