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

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

  factory PlantingRecordModel.fromJson(Map<String, dynamic> json) {
    return PlantingRecordModel(
      orderId: json['orderId'] ?? json['id'] ?? '',
      orderNo: json['orderNo'] ?? '',
      originalAdoptionId: json['originalAdoptionId'],
      treeCount: json['treeCount'] ?? 0,
      contributionPerTree: json['contributionPerTree']?.toString() ?? '0',
      totalContribution: json['totalContribution']?.toString() ?? json['totalAmount']?.toString() ?? '0',
      totalAmount: json['totalAmount']?.toString() ?? '0',
      status: _parseStatus(json['status']),
      adoptionDate: json['adoptionDate'] != null ? DateTime.tryParse(json['adoptionDate']) : null,
      createdAt: json['createdAt'] != null
          ? DateTime.parse(json['createdAt'])
          : DateTime.now(),
    );
  }

  static PlantingStatus _parseStatus(String? status) {
    switch (status?.toUpperCase()) {
      case 'CREATED':
        return PlantingStatus.created;
      case 'PAID':
        return PlantingStatus.paid;
      case 'FUND_ALLOCATED':
        return PlantingStatus.fundAllocated;
      case 'MINING_ENABLED':
        return PlantingStatus.miningEnabled;
      case 'CANCELLED':
        return PlantingStatus.cancelled;
      case 'EXPIRED':
        return PlantingStatus.expired;
      default:
        return PlantingStatus.created;
    }
  }
}

class PlantingSummaryModel extends PlantingSummary {
  const PlantingSummaryModel({
    required super.totalOrders,
    required super.totalTreeCount,
    required super.totalAmount,
    required super.effectiveTreeCount,
    required super.effectiveContribution,
    super.firstPlantingAt,
    super.lastPlantingAt,
  });

  factory PlantingSummaryModel.fromJson(Map<String, dynamic> json) {
    return PlantingSummaryModel(
      totalOrders: json['totalOrders'] ?? 0,
      totalTreeCount: json['totalTreeCount'] ?? 0,
      totalAmount: json['totalAmount']?.toString() ?? '0',
      effectiveTreeCount: json['effectiveTreeCount'] ?? 0,
      effectiveContribution: json['effectiveContribution']?.toString() ?? '0',
      firstPlantingAt: json['firstPlantingAt'] != null
          ? DateTime.tryParse(json['firstPlantingAt'])
          : null,
      lastPlantingAt: json['lastPlantingAt'] != null
          ? DateTime.tryParse(json['lastPlantingAt'])
          : null,
    );
  }
}

class PlantingLedgerPageModel extends PlantingLedgerPage {
  const PlantingLedgerPageModel({
    required super.summary,
    required super.items,
    required super.total,
    required super.page,
    required super.pageSize,
    required super.totalPages,
  });

  factory PlantingLedgerPageModel.fromJson(Map<String, dynamic> json) {
    final itemsList = json['items'] as List? ?? [];
    return PlantingLedgerPageModel(
      summary: PlantingSummaryModel.fromJson(json['summary'] ?? {}),
      items: itemsList.map((e) => PlantingRecordModel.fromJson(e)).toList(),
      total: json['total'] ?? 0,
      page: json['page'] ?? 1,
      pageSize: json['pageSize'] ?? 10,
      totalPages: json['totalPages'] ?? 0,
    );
  }
}
