class CouponModel {
  final String id;
  final String businessName;
  final String title;
  final String description;
  final DateTime? validUntil;
  final String? imageUrl;
  final String category;
  final bool isActive;
  final DateTime createdAt;

  CouponModel({
    required this.id,
    required this.businessName,
    required this.title,
    required this.description,
    this.validUntil,
    this.imageUrl,
    required this.category,
    required this.isActive,
    required this.createdAt,
  });

  factory CouponModel.fromMap(Map<String, dynamic> map) {
    return CouponModel(
      id: map['id'] as String,
      businessName: map['business_name'] as String,
      title: map['title'] as String,
      description: map['description'] as String? ?? '',
      validUntil: map['valid_until'] != null
          ? DateTime.parse(map['valid_until'])
          : null,
      imageUrl: map['image_url'] as String?,
      category: map['category'] as String,
      isActive: map['is_active'] as bool? ?? true,
      createdAt: DateTime.parse(map['created_at']),
    );
  }

  Map<String, dynamic> toMap() {
    return {
      'id': id,
      'business_name': businessName,
      'title': title,
      'description': description,
      'valid_until': validUntil?.toIso8601String(),
      'image_url': imageUrl,
      'category': category,
      'is_active': isActive,
      'created_at': createdAt.toIso8601String(),
    };
  }

  bool get isExpired {
    if (validUntil == null) return false;
    return DateTime.now().isAfter(validUntil!);
  }

  bool get isExpiringSoon {
    if (validUntil == null) return false;
    final now = DateTime.now();
    final difference = validUntil!.difference(now).inDays;
    return difference <= 3 && difference >= 0;
  }

  String get categoryDisplayName {
    switch (category.toLowerCase()) {
      case 'restaurantes':
        return 'Restaurantes';
      case 'tiendas':
        return 'Tiendas';
      case 'servicios':
        return 'Servicios';
      case 'entretenimiento':
        return 'Entretenimiento';
      case 'salud':
        return 'Salud';
      case 'belleza':
        return 'Belleza';
      default:
        return 'Otros';
    }
  }

  String get validUntilFormatted {
    if (validUntil == null) return 'Sin fecha de vencimiento';
    return '${validUntil!.day.toString().padLeft(2, '0')}/${validUntil!.month.toString().padLeft(2, '0')}/${validUntil!.year}';
  }

  CouponModel copyWith({
    String? id,
    String? businessName,
    String? title,
    String? description,
    DateTime? validUntil,
    String? imageUrl,
    String? category,
    bool? isActive,
    DateTime? createdAt,
  }) {
    return CouponModel(
      id: id ?? this.id,
      businessName: businessName ?? this.businessName,
      title: title ?? this.title,
      description: description ?? this.description,
      validUntil: validUntil ?? this.validUntil,
      imageUrl: imageUrl ?? this.imageUrl,
      category: category ?? this.category,
      isActive: isActive ?? this.isActive,
      createdAt: createdAt ?? this.createdAt,
    );
  }
}
