/// 用户模型
class User {
  final int id;
  final String username;
  final String? nickname;
  final String? avatar;
  final String? phone;
  final String? email;
  final int kycStatus;
  final int status;
  final DateTime? lastLoginTime;
  final DateTime? createTime;

  User({
    required this.id,
    required this.username,
    this.nickname,
    this.avatar,
    this.phone,
    this.email,
    required this.kycStatus,
    required this.status,
    this.lastLoginTime,
    this.createTime,
  });

  factory User.fromJson(Map<String, dynamic> json) {
    return User(
      id: json['id'] as int? ?? 0,
      username: json['username'] as String? ?? '',
      nickname: json['nickname'] as String?,
      avatar: json['avatar'] as String?,
      phone: json['phone'] as String?,
      email: json['email'] as String?,
      kycStatus: json['kycStatus'] as int? ?? 0,
      status: json['status'] as int? ?? 1,
      lastLoginTime: json['lastLoginTime'] != null
          ? DateTime.tryParse(json['lastLoginTime'])
          : null,
      createTime: json['createTime'] != null
          ? DateTime.tryParse(json['createTime'])
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'username': username,
      'nickname': nickname,
      'avatar': avatar,
      'phone': phone,
      'email': email,
      'kycStatus': kycStatus,
      'status': status,
      'lastLoginTime': lastLoginTime?.toIso8601String(),
      'createTime': createTime?.toIso8601String(),
    };
  }

  /// 获取头像显示文字（用户名首字母）
  String get avatarText =>
      username.isNotEmpty ? username.substring(0, 1).toUpperCase() : 'U';

  /// KYC 状态文字
  String get kycStatusText {
    switch (kycStatus) {
      case 0:
        return '未认证';
      case 1:
        return '审核中';
      case 2:
        return '已认证';
      case 3:
        return '认证失败';
      default:
        return '未知';
    }
  }
}
