class ReservationEntity {
  final String id;
  final String shop;
  final ReservationDiscount discount;
  final String user;
  final bool status;

  ReservationEntity({
    required this.id,
    required this.shop,
    required this.discount,
    required this.user,
    required this.status,
  });

  factory ReservationEntity.fromJson(Map<String, dynamic> json) {
    return ReservationEntity(
      id: json['ID'] ?? '',
      shop: json['Shop'] ?? '',
      discount: ReservationDiscount.fromJson(json['Discount'] ?? {}),
      user: json['User'] ?? '',
      status: json['Status'] ?? false,
    );
  }
}

class ReservationDiscount {
  final String id;
  final String name;
  final String typeName;
  final DateTime? endDate;
  final List<ReservationImage> images;


  ReservationDiscount({
    required this.id,
    required this.name,
    this.endDate,
    required this.images,
    required this.typeName,
  });

  factory ReservationDiscount.fromJson(Map<String, dynamic> json) {
    List<ReservationImage> _parseImages(dynamic imageList) {
      if (imageList is List) {
        return imageList.map((img) => ReservationImage.fromJson(img)).toList();
      }
      return [];
    }

    return ReservationDiscount(
      id: json['ID'] ?? '',
      name: json['Name'] ?? 'بدون نام',
      endDate: json['EndDate'] != null ? DateTime.tryParse(json['EndDate']) : null,
      images: _parseImages(json['Images']),
      typeName: json['Type']?['Name'] ?? 'نامشخص',
    );
  }
}

class ReservationImage {
  final String id;
  final String url;

  ReservationImage({
    required this.id,
    required this.url,
  });

  factory ReservationImage.fromJson(Map<String, dynamic> json) {
    return ReservationImage(
      id: json['_id'] ?? '',
      url: json['Url'] ?? '',
    );
  }
}
