/// 当前资金费率 + 下次结算时间
class FundingRateCurrent {
  /// 资金费率（小数，如 0.00005 表示 0.005%）
  final double rate;

  /// 下次结算时间（毫秒时间戳，服务端按 8 小时整点返回）
  final int refreshTime;

  const FundingRateCurrent({required this.rate, required this.refreshTime});

  factory FundingRateCurrent.fromJson(Map<String, dynamic> json) {
    return FundingRateCurrent(
      rate: (json['fundingRate'] as num?)?.toDouble() ?? 0,
      refreshTime: (json['refreshTime'] as num?)?.toInt() ?? 0,
    );
  }
}

/// 资金费率历史记录
class FundingRateHistoryItem {
  final DateTime fundingTime;

  /// 资金费率（小数，如 0.00005 表示 0.005%）
  final double rate;

  const FundingRateHistoryItem({
    required this.fundingTime,
    required this.rate,
  });

  factory FundingRateHistoryItem.fromJson(Map<String, dynamic> json) {
    final raw = json['fundingTime'];
    DateTime time;
    if (raw is int) {
      time = DateTime.fromMillisecondsSinceEpoch(raw);
    } else if (raw is String) {
      time = DateTime.tryParse(raw) ?? DateTime.fromMillisecondsSinceEpoch(0);
    } else {
      time = DateTime.fromMillisecondsSinceEpoch(0);
    }
    return FundingRateHistoryItem(
      fundingTime: time,
      rate: (json['fundingRate'] as num?)?.toDouble() ?? 0,
    );
  }
}
