/// 资产总览模型
class AssetOverview {
  final String totalAsset;
  final String fundBalance;
  final String tradeBalance;
  final String totalProfit;

  AssetOverview({
    required this.totalAsset,
    required this.fundBalance,
    required this.tradeBalance,
    required this.totalProfit,
  });

  factory AssetOverview.fromJson(Map<String, dynamic> json) {
    return AssetOverview(
      totalAsset: json['totalAsset']?.toString() ?? '0.00',
      fundBalance: json['fundBalance']?.toString() ?? '0.00',
      tradeBalance: json['tradeBalance']?.toString() ?? '0.00',
      totalProfit: json['totalProfit']?.toString() ?? '0.00',
    );
  }
}

/// 资金账户模型
class AccountFund {
  final int id;
  final int userId;
  final String balance;
  final String frozenBalance;
  final DateTime? updateTime;

  AccountFund({
    required this.id,
    required this.userId,
    required this.balance,
    required this.frozenBalance,
    this.updateTime,
  });

  factory AccountFund.fromJson(Map<String, dynamic> json) {
    return AccountFund(
      id: json['id'] as int? ?? 0,
      userId: json['userId'] as int? ?? 0,
      balance: json['balance']?.toString() ?? '0.00',
      frozenBalance: json['frozenBalance']?.toString() ?? '0.00',
      updateTime: json['updateTime'] != null
          ? DateTime.tryParse(json['updateTime'])
          : null,
    );
  }
}

/// 交易账户模型（持仓）
class AccountTrade {
  final int id;
  final int userId;
  final String coinCode;
  final String quantity;
  final String avgPrice;
  final String totalCost;
  final String currentValue;
  final String profit;
  final double profitRate;
  final DateTime? updateTime;

  AccountTrade({
    required this.id,
    required this.userId,
    required this.coinCode,
    required this.quantity,
    required this.avgPrice,
    required this.totalCost,
    required this.currentValue,
    required this.profit,
    required this.profitRate,
    this.updateTime,
  });

  factory AccountTrade.fromJson(Map<String, dynamic> json) {
    return AccountTrade(
      id: json['id'] as int? ?? 0,
      userId: json['userId'] as int? ?? 0,
      coinCode: json['coinCode'] as String? ?? '',
      quantity: json['quantity']?.toString() ?? '0',
      avgPrice: json['avgPrice']?.toString() ?? '0.00',
      totalCost: json['totalCost']?.toString() ?? '0.00',
      currentValue: json['currentValue']?.toString() ?? '0.00',
      profit: json['profit']?.toString() ?? '0.00',
      profitRate: (json['profitRate'] as num?)?.toDouble() ?? 0,
      updateTime: json['updateTime'] != null
          ? DateTime.tryParse(json['updateTime'])
          : null,
    );
  }

  /// 格式化盈亏率
  String get formattedProfitRate {
    final prefix = profitRate >= 0 ? '+' : '';
    return '$prefix${profitRate.toStringAsFixed(2)}%';
  }

  /// 是否盈利
  bool get isProfit => profitRate >= 0;
}

/// 资金流水模型
class AccountFlow {
  final int id;
  final int userId;
  final String flowType;
  final String amount;
  final String balanceBefore;
  final String balanceAfter;
  final String remark;
  final DateTime? createTime;

  AccountFlow({
    required this.id,
    required this.userId,
    required this.flowType,
    required this.amount,
    required this.balanceBefore,
    required this.balanceAfter,
    required this.remark,
    this.createTime,
  });

  factory AccountFlow.fromJson(Map<String, dynamic> json) {
    return AccountFlow(
      id: json['id'] as int? ?? 0,
      userId: json['userId'] as int? ?? 0,
      flowType: json['flowType']?.toString() ?? '',
      amount: json['amount']?.toString() ?? '0.00',
      balanceBefore: json['balanceBefore']?.toString() ?? '0.00',
      balanceAfter: json['balanceAfter']?.toString() ?? '0.00',
      remark: json['remark']?.toString() ?? '',
      createTime: json['createTime'] != null
          ? DateTime.tryParse(json['createTime'])
          : null,
    );
  }

  /// 流水类型文字
  String get flowTypeText {
    switch (flowType) {
      case '1':
        return '充值';
      case '2':
        return '提现';
      case '3':
        return '转入交易账户';
      case '4':
        return '从交易账户转出';
      case '5':
        return '卖出收入';
      case '6':
        return '买入支出';
      default:
        return '未知';
    }
  }

  /// 是否为收入
  bool get isIncome => ['1', '3', '5'].contains(flowType);
}
