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

class ContributionRecordModel extends ContributionRecord {
  const ContributionRecordModel({
    required super.id,
    required super.sourceType,
    required super.sourceAdoptionId,
    super.sourceAccountSequence,
    required super.treeCount,
    required super.baseContribution,
    required super.distributionRate,
    super.levelDepth,
    super.bonusTier,
    required super.finalContribution,
    required super.effectiveDate,
    required super.expireDate,
    required super.isExpired,
    required super.createdAt,
  });

  factory ContributionRecordModel.fromJson(Map<String, dynamic> json) {
    return ContributionRecordModel(
      id: json['id']?.toString() ?? '',
      sourceType: _parseSourceType(json['sourceType']),
      sourceAdoptionId: json['sourceAdoptionId']?.toString() ?? '',
      sourceAccountSequence: json['sourceAccountSequence']?.toString(),
      treeCount: json['treeCount'] ?? 0,
      baseContribution: json['baseContribution']?.toString() ?? '0',
      distributionRate: json['distributionRate']?.toString() ?? '0',
      levelDepth: json['levelDepth'],
      bonusTier: json['bonusTier'],
      finalContribution: json['finalContribution']?.toString() ?? '0',
      effectiveDate: DateTime.parse(json['effectiveDate']),
      expireDate: DateTime.parse(json['expireDate']),
      isExpired: json['isExpired'] == true,
      createdAt: DateTime.parse(json['createdAt']),
    );
  }

  static ContributionSourceType _parseSourceType(String? type) {
    switch (type) {
      case 'PERSONAL':
        return ContributionSourceType.personal;
      case 'TEAM_LEVEL':
        return ContributionSourceType.teamLevel;
      case 'TEAM_BONUS':
        return ContributionSourceType.teamBonus;
      default:
        return ContributionSourceType.personal;
    }
  }
}

class ContributionRecordsPageModel extends ContributionRecordsPage {
  const ContributionRecordsPageModel({
    required super.data,
    required super.total,
    required super.page,
    required super.pageSize,
  });

  factory ContributionRecordsPageModel.fromJson(Map<String, dynamic> json) {
    final dataList = (json['data'] as List<dynamic>?) ?? [];
    return ContributionRecordsPageModel(
      data: dataList.map((e) => ContributionRecordModel.fromJson(e)).toList(),
      total: json['total'] ?? 0,
      page: json['page'] ?? 1,
      pageSize: json['pageSize'] ?? 50,
    );
  }
}
