import 'package:equatable/equatable.dart';

/// 参与状态枚举
enum PlantingStatus {
  created,
  paid,
  fundAllocated,
  miningEnabled,
  cancelled,
  expired,
}

/// 参与记录
class PlantingRecord extends Equatable {
  /// 订单ID
  final String orderId;
  /// 订单号
  final String orderNo;
  /// 原始参与ID
  final String? originalAdoptionId;
  /// 参与数量
  final int treeCount;
  /// 单棵算力
  final String contributionPerTree;
  /// 总算力
  final String totalContribution;
  /// 参与金额
  final String totalAmount;
  /// 状态
  final PlantingStatus status;
  /// 参与日期
  final DateTime? adoptionDate;
  /// 创建时间
  final DateTime createdAt;

  const PlantingRecord({
    required this.orderId,
    required this.orderNo,
    this.originalAdoptionId,
    required this.treeCount,
    required this.contributionPerTree,
    required this.totalContribution,
    required this.totalAmount,
    required this.status,
    this.adoptionDate,
    required this.createdAt,
  });

  /// 状态显示文本
  String get statusText {
    switch (status) {
      case PlantingStatus.created:
        return '已创建';
      case PlantingStatus.paid:
        return '已支付';
      case PlantingStatus.fundAllocated:
        return '资金已分配';
      case PlantingStatus.miningEnabled:
        return '已开始';
      case PlantingStatus.cancelled:
        return '已取消';
      case PlantingStatus.expired:
        return '已过期';
    }
  }

  /// 是否为有效状态
  bool get isActive =>
      status == PlantingStatus.miningEnabled ||
      status == PlantingStatus.paid ||
      status == PlantingStatus.fundAllocated;

  @override
  List<Object?> get props => [orderId, orderNo, treeCount, status];
}

/// 参与汇总
class PlantingSummary extends Equatable {
  /// 总订单数
  final int totalOrders;
  /// 总参与量
  final int totalTreeCount;
  /// 总金额（理论贡献值）
  final String totalAmount;
  /// 有效参与量
  final int effectiveTreeCount;
  /// 有效贡献值（实际个人算力）
  final String effectiveContribution;
  /// 首次参与时间
  final DateTime? firstPlantingAt;
  /// 最近参与时间
  final DateTime? lastPlantingAt;

  const PlantingSummary({
    required this.totalOrders,
    required this.totalTreeCount,
    required this.totalAmount,
    required this.effectiveTreeCount,
    required this.effectiveContribution,
    this.firstPlantingAt,
    this.lastPlantingAt,
  });

  @override
  List<Object?> get props => [
        totalOrders,
        totalTreeCount,
        totalAmount,
        effectiveTreeCount,
        effectiveContribution,
        firstPlantingAt,
        lastPlantingAt,
      ];
}

/// 参与分类账分页数据
class PlantingLedgerPage extends Equatable {
  final PlantingSummary summary;
  final List<PlantingRecord> items;
  final int total;
  final int page;
  final int pageSize;
  final int totalPages;

  const PlantingLedgerPage({
    required this.summary,
    required this.items,
    required this.total,
    required this.page,
    required this.pageSize,
    required this.totalPages,
  });

  @override
  List<Object?> get props => [summary, items, total, page, pageSize, totalPages];
}
