import 'payment_card_suggestion.dart';

class Subscription {
  final String id;
  final String serviceName;
  final double monthlyCost;
  final String billingCycle;
  final DateTime nextBillingDate;
  final String? category;
  final String? notes;
  final int repeatCount;
  final DateTime? lastPaymentDate;
  final String? websiteUrl;
  final String currency;
  final String? paymentCardId;
  final PaymentCardSuggestion? paymentCardSuggestion;
  final String? rawMessage;

  Subscription({
    required this.id,
    required this.serviceName,
    required this.monthlyCost,
    required this.billingCycle,
    required this.nextBillingDate,
    this.category,
    this.notes,
    this.repeatCount = 1,
    this.lastPaymentDate,
    this.websiteUrl,
    this.currency = 'KRW',
    this.paymentCardId,
    this.paymentCardSuggestion,
    this.rawMessage,
  });

  Subscription copyWith({
    String? id,
    String? serviceName,
    double? monthlyCost,
    String? billingCycle,
    DateTime? nextBillingDate,
    String? category,
    String? notes,
    int? repeatCount,
    DateTime? lastPaymentDate,
    String? websiteUrl,
    String? currency,
    String? paymentCardId,
    PaymentCardSuggestion? paymentCardSuggestion,
    String? rawMessage,
  }) {
    return Subscription(
      id: id ?? this.id,
      serviceName: serviceName ?? this.serviceName,
      monthlyCost: monthlyCost ?? this.monthlyCost,
      billingCycle: billingCycle ?? this.billingCycle,
      nextBillingDate: nextBillingDate ?? this.nextBillingDate,
      category: category ?? this.category,
      notes: notes ?? this.notes,
      repeatCount: repeatCount ?? this.repeatCount,
      lastPaymentDate: lastPaymentDate ?? this.lastPaymentDate,
      websiteUrl: websiteUrl ?? this.websiteUrl,
      currency: currency ?? this.currency,
      paymentCardId: paymentCardId ?? this.paymentCardId,
      paymentCardSuggestion: paymentCardSuggestion ??
          (this.paymentCardSuggestion != null
              ? PaymentCardSuggestion(
                  issuerName: this.paymentCardSuggestion!.issuerName,
                  last4: this.paymentCardSuggestion!.last4,
                  source: this.paymentCardSuggestion!.source,
                )
              : null),
      rawMessage: rawMessage ?? this.rawMessage,
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'serviceName': serviceName,
      'monthlyCost': monthlyCost,
      'billingCycle': billingCycle,
      'nextBillingDate': nextBillingDate.toIso8601String(),
      'category': category,
      'notes': notes,
      'repeatCount': repeatCount,
      'lastPaymentDate': lastPaymentDate?.toIso8601String(),
      'websiteUrl': websiteUrl,
      'currency': currency,
      'paymentCardId': paymentCardId,
      'paymentCardSuggestionIssuer': paymentCardSuggestion?.issuerName,
      'paymentCardSuggestionLast4': paymentCardSuggestion?.last4,
      'paymentCardSuggestionSource': paymentCardSuggestion?.source,
      'rawMessage': rawMessage,
    };
  }

  factory Subscription.fromMap(Map<String, dynamic> map) {
    return Subscription(
      id: map['id'] as String,
      serviceName: map['serviceName'] as String,
      monthlyCost: map['monthlyCost'] as double,
      billingCycle: map['billingCycle'] as String,
      nextBillingDate: DateTime.parse(map['nextBillingDate'] as String),
      category: map['category'] as String?,
      notes: map['notes'] as String?,
      repeatCount: (map['repeatCount'] as num?)?.toInt() ?? 1,
      lastPaymentDate: map['lastPaymentDate'] != null
          ? DateTime.parse(map['lastPaymentDate'] as String)
          : null,
      websiteUrl: map['websiteUrl'] as String?,
      currency: map['currency'] as String? ?? 'KRW',
      paymentCardId: map['paymentCardId'] as String?,
      paymentCardSuggestion: map['paymentCardSuggestionIssuer'] != null
          ? PaymentCardSuggestion(
              issuerName: map['paymentCardSuggestionIssuer'] as String,
              last4: map['paymentCardSuggestionLast4'] as String?,
              source: map['paymentCardSuggestionSource'] as String?,
            )
          : null,
      rawMessage: map['rawMessage'] as String?,
    );
  }

  // 주기적 결제 여부 확인
  bool get isRecurring => repeatCount > 1;
}
