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;

  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',
  });

  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,
    };
  }

  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',
    );
  }

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