import '../../domain/entities/batch_mining_record.dart';

class BatchMiningRecordModel extends BatchMiningRecord {
  const BatchMiningRecordModel({
    required super.id,
    required super.accountSequence,
    required super.batch,
    required super.phase,
    required super.treeCount,
    required super.daysInPhase,
    required super.preMineDays,
    required super.amount,
    super.remark,
    required super.createdAt,
  });

  factory BatchMiningRecordModel.fromJson(Map<String, dynamic> json) {
    return BatchMiningRecordModel(
      id: json['id']?.toString() ?? '',
      accountSequence: json['accountSequence']?.toString() ?? '',
      batch: json['batch'] as int? ?? 0,
      phase: json['phase'] as int? ?? 0,
      treeCount: json['treeCount'] as int? ?? 0,
      daysInPhase: json['daysInPhase'] as int? ?? 0,
      preMineDays: json['preMineDays'] as int? ?? 0,
      amount: json['amount']?.toString() ?? '0',
      remark: json['remark']?.toString(),
      createdAt: json['createdAt'] != null
          ? DateTime.parse(json['createdAt'].toString())
          : DateTime.now(),
    );
  }
}

class BatchMiningRecordsPageModel extends BatchMiningRecordsPage {
  const BatchMiningRecordsPageModel({
    required super.items,
    required super.total,
    required super.page,
    required super.pageSize,
    required super.totalPages,
  });

  factory BatchMiningRecordsPageModel.fromJson(
    Map<String, dynamic> json,
    int page,
    int pageSize,
  ) {
    final rawItems = json['records'] as List? ?? json['data'] as List? ?? json['items'] as List? ?? [];
    final items = rawItems
        .map((item) => BatchMiningRecordModel.fromJson(item as Map<String, dynamic>))
        .toList();
    final total = json['total'] as int? ?? items.length;
    return BatchMiningRecordsPageModel(
      items: items,
      total: total,
      page: page,
      pageSize: pageSize,
      totalPages: (total / pageSize).ceil(),
    );
  }
}
