class ReferralInfo {
  final String referralCode;
  final String shareUrl;
  final int directCount;
  final int activeCount;
  final int pendingCreditCents;
  final int totalEarnedCents;
  final int totalAppliedCents;

  const ReferralInfo({
    required this.referralCode,
    required this.shareUrl,
    required this.directCount,
    required this.activeCount,
    required this.pendingCreditCents,
    required this.totalEarnedCents,
    required this.totalAppliedCents,
  });

  factory ReferralInfo.fromJson(Map<String, dynamic> json) => ReferralInfo(
        referralCode: json['referralCode'] as String? ?? '',
        shareUrl: json['shareUrl'] as String? ?? '',
        directCount: json['directCount'] as int? ?? 0,
        activeCount: json['activeCount'] as int? ?? 0,
        pendingCreditCents: json['pendingCreditCents'] as int? ?? 0,
        totalEarnedCents: json['totalEarnedCents'] as int? ?? 0,
        totalAppliedCents: json['totalAppliedCents'] as int? ?? 0,
      );

  String get pendingCreditFormatted =>
      '\$${(pendingCreditCents / 100).toStringAsFixed(2)}';

  String get totalEarnedFormatted =>
      '\$${(totalEarnedCents / 100).toStringAsFixed(2)}';
}

class ReferralItem {
  final String id;
  final String referredTenantId;
  final String referralCode;
  final String status;  // PENDING | ACTIVE | REWARDED | EXPIRED
  final int level;
  final DateTime registeredAt;
  final DateTime? activatedAt;

  const ReferralItem({
    required this.id,
    required this.referredTenantId,
    required this.referralCode,
    required this.status,
    required this.level,
    required this.registeredAt,
    this.activatedAt,
  });

  factory ReferralItem.fromJson(Map<String, dynamic> json) => ReferralItem(
        id: json['id'] as String,
        referredTenantId: json['referredTenantId'] as String? ?? '',
        referralCode: json['referralCode'] as String? ?? '',
        status: json['status'] as String? ?? 'PENDING',
        level: json['level'] as int? ?? 1,
        registeredAt: DateTime.parse(json['registeredAt'] as String),
        activatedAt: json['activatedAt'] != null
            ? DateTime.parse(json['activatedAt'] as String)
            : null,
      );

  bool get isActive => status == 'ACTIVE' || status == 'REWARDED';
}

class RewardItem {
  final String id;
  final int amountCents;
  final String amountFormatted;
  final String rewardType;
  final String triggerType;
  final String status;  // PENDING | APPLIED | EXPIRED
  final String? sourceInvoiceId;
  final int? recurringMonth;
  final DateTime createdAt;
  final DateTime? appliedAt;

  const RewardItem({
    required this.id,
    required this.amountCents,
    required this.amountFormatted,
    required this.rewardType,
    required this.triggerType,
    required this.status,
    this.sourceInvoiceId,
    this.recurringMonth,
    required this.createdAt,
    this.appliedAt,
  });

  factory RewardItem.fromJson(Map<String, dynamic> json) => RewardItem(
        id: json['id'] as String,
        amountCents: json['amountCents'] as int? ?? 0,
        amountFormatted: json['amountFormatted'] as String? ?? '\$0.00',
        rewardType: json['rewardType'] as String? ?? 'CREDIT',
        triggerType: json['triggerType'] as String? ?? 'FIRST_PAYMENT',
        status: json['status'] as String? ?? 'PENDING',
        sourceInvoiceId: json['sourceInvoiceId'] as String?,
        recurringMonth: json['recurringMonth'] as int?,
        createdAt: DateTime.parse(json['createdAt'] as String),
        appliedAt: json['appliedAt'] != null
            ? DateTime.parse(json['appliedAt'] as String)
            : null,
      );

  String get triggerLabel =>
      triggerType == 'FIRST_PAYMENT' ? '首次付款奖励' : '续订奖励(第${recurringMonth ?? 1}月)';
}
