/// 用户信息（对应 Android UserInfoEntity）
class UserInfo {
  final int id;
  final String? mobile;
  final String? phone;          // 别名：同 mobile
  final String? username;       // 登录账号
  final String? name;
  final String? realName;       // 别名：同 name（认证用）
  final String? avatar;
  final String? idCard;
  final String? driverNo;       // 司机编号
  final String? companyName;    // 企业名称
  final String? company;        // 别名：同 companyName
  final String? address;        // 联系地址
  final String? email;          // 邮箱
  final String? authType;       // personal / enterprise / driver
  final String? authStatus;     // none / pending / approved / rejected
  final String? wxOpenid;
  final String? wxUnionid;
  final double? balance;
  final String? token;
  final int? todayOrders;       // 今日订单数
  final int? totalOrders;       // 累计订单数

  const UserInfo({
    required this.id,
    this.mobile,
    this.phone,
    this.username,
    this.name,
    this.realName,
    this.avatar,
    this.idCard,
    this.driverNo,
    this.companyName,
    this.company,
    this.address,
    this.email,
    this.authType,
    this.authStatus,
    this.wxOpenid,
    this.wxUnionid,
    this.balance,
    this.token,
    this.todayOrders,
    this.totalOrders,
  });

  bool get isAuthenticated =>
      authStatus == 'approved' && (authType?.isNotEmpty ?? false);

  factory UserInfo.fromJson(Map<String, dynamic> json) {
    return UserInfo(
      id: json['id'] as int? ?? 0,
      mobile: json['mobile'] as String?,
      phone: json['phone'] as String? ?? json['mobile'] as String?,
      username: json['username'] as String?,
      name: json['name'] as String?,
      realName: json['real_name'] as String? ?? json['name'] as String?,
      avatar: json['avatar'] as String?,
      idCard: json['id_card'] as String?,
      driverNo: json['driver_no'] as String?,
      companyName: json['company_name'] as String?,
      company: json['company'] as String? ?? json['company_name'] as String?,
      address: json['address'] as String?,
      email: json['email'] as String?,
      authType: json['auth_type'] as String?,
      authStatus: json['auth_status'] as String?,
      wxOpenid: json['wx_openid'] as String?,
      wxUnionid: json['wx_unionid'] as String?,
      balance: (json['balance'] as num?)?.toDouble(),
      token: json['token'] as String?,
      todayOrders: json['today_orders'] as int?,
      totalOrders: json['total_orders'] as int?,
    );
  }

  Map<String, dynamic> toJson() => {
        'id': id,
        'mobile': mobile,
        'phone': phone,
        'username': username,
        'name': name,
        'real_name': realName,
        'avatar': avatar,
        'id_card': idCard,
        'driver_no': driverNo,
        'company_name': companyName,
        'company': company,
        'address': address,
        'email': email,
        'auth_type': authType,
        'auth_status': authStatus,
        'wx_openid': wxOpenid,
        'wx_unionid': wxUnionid,
        'balance': balance,
        'token': token,
        'today_orders': todayOrders,
        'total_orders': totalOrders,
      };
}

/// 登录返回（对应 Android LoginTokenEntity）
class LoginToken {
  final String accessToken;
  final String? refreshToken;
  final UserInfo? user;

  const LoginToken({
    required this.accessToken,
    this.refreshToken,
    this.user,
  });

  factory LoginToken.fromJson(Map<String, dynamic> json) {
    return LoginToken(
      accessToken: json['access_token'] as String? ?? '',
      refreshToken: json['refresh_token'] as String?,
      user: json['user'] != null
          ? UserInfo.fromJson(json['user'] as Map<String, dynamic>)
          : null,
    );
  }
}