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

class ContributionModel extends Contribution {
  const ContributionModel({
    required super.status,
    required super.message,
    required super.accountSequence,
    required super.personalContribution,
    required super.teamLevelContribution,
    required super.teamBonusContribution,
    required super.totalContribution,
    required super.hasAdopted,
    required super.directReferralAdoptedCount,
    required super.unlockedLevelDepth,
    required super.unlockedBonusTiers,
    required super.isCalculated,
    super.lastCalculatedAt,
  });

  factory ContributionModel.fromJson(Map<String, dynamic> json) {
    return ContributionModel(
      status: _parseStatus(json['status']),
      message: json['message']?.toString() ?? '',
      accountSequence: json['accountSequence']?.toString() ?? '',
      personalContribution: json['personalContribution']?.toString() ?? '0',
      teamLevelContribution: json['teamLevelContribution']?.toString() ?? '0',
      teamBonusContribution: json['teamBonusContribution']?.toString() ?? '0',
      totalContribution: json['totalContribution']?.toString() ?? '0',
      hasAdopted: json['hasAdopted'] == true,
      directReferralAdoptedCount: json['directReferralAdoptedCount'] ?? 0,
      unlockedLevelDepth: json['unlockedLevelDepth'] ?? 0,
      unlockedBonusTiers: json['unlockedBonusTiers'] ?? 0,
      isCalculated: json['isCalculated'] == true,
      lastCalculatedAt: json['lastCalculatedAt'] != null
          ? DateTime.tryParse(json['lastCalculatedAt'].toString())
          : null,
    );
  }

  static ContributionAccountStatus _parseStatus(String? status) {
    switch (status) {
      case 'ACTIVE':
        return ContributionAccountStatus.active;
      case 'INACTIVE':
        return ContributionAccountStatus.inactive;
      case 'USER_NOT_FOUND':
        return ContributionAccountStatus.userNotFound;
      default:
        return ContributionAccountStatus.inactive;
    }
  }
}
