/// 提现/转账记录 - GET uc/withdraw/record | uc/withdraw/transfer/record
class WithdrawRecord {
  final String id;

  /// "0"审核中 "1"放币中 "2"失败 "3"成功 "4"用户取消
  final String status;

  final String address;

  /// 总金额（JSON: totalAmount）—— 链上提币使用
  final String amount;

  /// 到账/转账金额（JSON: amount）—— 内部转账使用
  final String transferAmount;

  final String createTime;
  final String dealTime;

  /// 交易哈希（JSON: transactionNumber）
  final String transactionHashId;

  final WithdrawRecordCoin? coin;
  final String memberId;

  /// 是否为内部转账（本地标记，非后端字段）
  final bool isTransfer;

  const WithdrawRecord({
    this.id = '',
    this.status = '0',
    this.address = '',
    this.amount = '0',
    this.transferAmount = '0',
    this.createTime = '',
    this.dealTime = '',
    this.transactionHashId = '',
    this.coin,
    this.memberId = '',
    this.isTransfer = false,
  });

  WithdrawRecord copyWith({bool? isTransfer}) => WithdrawRecord(
        id: id,
        status: status,
        address: address,
        amount: amount,
        transferAmount: transferAmount,
        createTime: createTime,
        dealTime: dealTime,
        transactionHashId: transactionHashId,
        coin: coin,
        memberId: memberId,
        isTransfer: isTransfer ?? this.isTransfer,
      );

  bool get canCancel => status == '0';
  bool get isSuccess => status == '3';

  String get statusText {
    switch (status) {
      case '0': return '审核中';
      case '1': return '放币中';
      case '2': return '失败';
      case '3': return '成功';
      case '4': return '取消提现';
      default: return '未知';
    }
  }

  factory WithdrawRecord.fromJson(Map<String, dynamic> json) {
    return WithdrawRecord(
      id: json['id']?.toString() ?? '',
      status: json['status']?.toString() ?? '0',
      address: json['address']?.toString() ?? '',
      amount: json['totalAmount']?.toString() ?? '0',
      transferAmount: json['amount']?.toString() ?? '0',
      createTime: json['createTime']?.toString() ?? '',
      dealTime: json['dealTime']?.toString() ?? '',
      transactionHashId: json['transactionNumber']?.toString() ?? '',
      coin: json['coin'] != null
          ? WithdrawRecordCoin.fromJson(json['coin'] as Map<String, dynamic>)
          : null,
      memberId: json['memberId']?.toString() ?? '',
    );
  }
}

/// 提现记录币种信息
class WithdrawRecordCoin {
  /// 币种代号（JSON: unit）如 TUSDT / EUSDT / USDT
  final String coinName;

  /// 中文别名（JSON: nameCn）如 USDT-TRC20
  final String aliasName;

  /// 手续费（JSON: withdrawFeeValue）
  final String fee;

  const WithdrawRecordCoin({
    this.coinName = '',
    this.aliasName = '',
    this.fee = '0',
  });

  /// 网络名称：TUSDT→TRC20, EUSDT→ERC20, BEP20→BEP20
  String get networkName {
    if (coinName == 'TUSDT') return 'TRC20';
    if (coinName == 'EUSDT') return 'ERC20';
    if (coinName.contains('BEP20')) return 'BEP20';
    // 从 aliasName 中提取，如 "USDT-TRC20" → "TRC20"
    if (aliasName.contains('-')) return aliasName.split('-').last;
    return '';
  }

  /// 基础币种显示名：TUSDT→USDT，或从 aliasName 取 "-" 前缀
  String get baseCoinDisplay {
    if (aliasName.contains('-')) return aliasName.split('-').first;
    // 从 unit 中提取基础名：TUSDT/EUSDT → USDT
    if (coinName.length > 4 && coinName.endsWith('USDT')) return 'USDT';
    return coinName;
  }

  factory WithdrawRecordCoin.fromJson(Map<String, dynamic> json) {
    return WithdrawRecordCoin(
      coinName: json['unit']?.toString() ?? '',
      aliasName: json['nameCn']?.toString() ?? '',
      fee: json['withdrawFeeValue']?.toString() ?? '0',
    );
  }
}
