class StakingOrderItem {
  final int id;
  final String coinUnit;
  final String amount;
  final String releasedAmount;
  final int status;
  final String? stakingTime;
  final String? unlockTime;
  final String? lastReleaseTime;
  final String? createTime;

  const StakingOrderItem({
    required this.id,
    required this.coinUnit,
    required this.amount,
    required this.releasedAmount,
    required this.status,
    this.stakingTime,
    this.unlockTime,
    this.lastReleaseTime,
    this.createTime,
  });

  factory StakingOrderItem.fromJson(Map<String, dynamic> json) {
    return StakingOrderItem(
      id: int.tryParse('${json['id'] ?? 0}') ?? 0,
      coinUnit: '${json['coinUnit'] ?? ''}'.toUpperCase(),
      amount: '${json['amount'] ?? '0'}',
      releasedAmount: '${json['releasedAmount'] ?? '0'}',
      status: int.tryParse('${json['status'] ?? 0}') ?? 0,
      stakingTime: json['stakingTime']?.toString(),
      unlockTime: json['unlockTime']?.toString(),
      lastReleaseTime: json['lastReleaseTime']?.toString(),
      createTime: json['createTime']?.toString(),
    );
  }

  /// 待释放 = 锁仓总量 - 已释放
  double get pendingAmount {
    final total = double.tryParse(amount) ?? 0;
    final released = double.tryParse(releasedAmount) ?? 0;
    final pending = total - released;
    if (!pending.isFinite || pending <= 0) {
      return 0;
    }
    return pending;
  }

  String get displayStartTime {
    final raw = stakingTime ?? createTime;
    if (raw == null || raw.isEmpty) {
      return '-';
    }
    return raw.replaceFirst('T', ' ').substring(0, raw.length >= 19 ? 19 : raw.length);
  }
}

class StakingOrderPage {
  final List<StakingOrderItem> content;
  final int totalPages;
  final int pageNo;

  const StakingOrderPage({
    required this.content,
    required this.totalPages,
    required this.pageNo,
  });
}

/// 锁仓订单状态筛选（对齐 Web StakingOrderStatusFilter）
enum StakingOrderStatusFilter {
  all,
  subscribing,
  releasing,
  completed,
}

extension StakingOrderStatusFilterX on StakingOrderStatusFilter {
  String? get apiStatus => switch (this) {
        StakingOrderStatusFilter.all => null,
        StakingOrderStatusFilter.subscribing => '0',
        StakingOrderStatusFilter.releasing => '1',
        StakingOrderStatusFilter.completed => '2',
      };
}
