/// 跟单账户权益
class CopyAccount {
  final double equity;            // 总权益 USDT
  final double todayPnl;          // 今日盈亏 USDT
  final double totalPnl;          // 总盈亏 USDT
  final double todayFee;          // 今日手续费 USDT
  final double cumulativePnl;     // 累计跟单收益 USDT
  final double availableBalance;  // 可用资产 USDT
  final double unrealizedPnl;     // 未实现盈亏 USDT

  const CopyAccount({
    required this.equity,
    required this.todayPnl,
    required this.totalPnl,
    required this.todayFee,
    this.cumulativePnl = 0,
    this.availableBalance = 0,
    this.unrealizedPnl = 0,
  });

  CopyAccount copyWith({double? unrealizedPnl}) => CopyAccount(
    equity: equity,
    todayPnl: todayPnl,
    totalPnl: totalPnl,
    todayFee: todayFee,
    cumulativePnl: cumulativePnl,
    availableBalance: availableBalance,
    unrealizedPnl: unrealizedPnl ?? this.unrealizedPnl,
  );

  /// 从 API follow-wallet/get 响应构建
  /// Android 字段映射（FollowRes.kt FollowWallet）：
  ///   totalRevenue   → 累计跟单收益（Kotlin 属性名 totalProfit，但 JSON key 是 totalRevenue）
  ///   currentRevenue → 未实现盈亏
  ///   currentCapital → 账户权益
  ///   availableBalance → 可用资产
  factory CopyAccount.fromApi(Map<String, dynamic> json) {
    double _d(String key) =>
        double.tryParse((json[key] ?? '0').toString()) ?? 0.0;
    return CopyAccount(
      equity: _d('currentCapital'),
      todayPnl: _d('currentRevenue'),
      totalPnl: _d('totalRevenue'),
      todayFee: 0,
      cumulativePnl: _d('totalRevenue'),
      availableBalance: _d('availableBalance'),
      unrealizedPnl: _d('currentRevenue'),
    );
  }
}
