class Reservation {
  final String id;
  final String status;
  final DateTime startDate;
  final DateTime endDate;
  final double totalAmount;
  final String paymentStatus;
  final String? vehicleId;
  final String? customerId;
  final VehicleSummary? vehicle;
  final CustomerSummary? customer;
  final DateTime createdAt;
  final String? source;
  final String? contractNumber;
  final String? invoiceNumber;

  const Reservation({
    required this.id,
    required this.status,
    required this.startDate,
    required this.endDate,
    required this.totalAmount,
    required this.paymentStatus,
    this.vehicleId,
    this.customerId,
    this.vehicle,
    this.customer,
    required this.createdAt,
    this.source,
    this.contractNumber,
    this.invoiceNumber,
  });

  int get days => endDate.difference(startDate).inDays;

  factory Reservation.fromJson(Map<String, dynamic> json) => Reservation(
        id: json['id'] as String,
        status: json['status'] as String,
        startDate: DateTime.parse(json['startDate'] as String),
        endDate: DateTime.parse(json['endDate'] as String),
        totalAmount: (json['totalAmount'] as num?)?.toDouble() ?? 0,
        paymentStatus: json['paymentStatus'] as String? ?? 'PENDING',
        vehicleId: json['vehicleId'] as String?,
        customerId: json['customerId'] as String?,
        vehicle: json['vehicle'] != null
            ? VehicleSummary.fromJson(
                json['vehicle'] as Map<String, dynamic>)
            : null,
        customer: json['customer'] != null
            ? CustomerSummary.fromJson(
                json['customer'] as Map<String, dynamic>)
            : null,
        createdAt: DateTime.parse(
            json['createdAt'] as String? ?? DateTime.now().toIso8601String()),
        source: json['source'] as String?,
        contractNumber: json['contractNumber'] as String?,
        invoiceNumber: json['invoiceNumber'] as String?,
      );
}

class VehicleSummary {
  final String id;
  final String make;
  final String model;
  final int year;
  final String licensePlate;
  final List<String> photos;

  const VehicleSummary({
    required this.id,
    required this.make,
    required this.model,
    required this.year,
    required this.licensePlate,
    required this.photos,
  });

  String get displayName => '$year $make $model';
  String? get primaryPhoto => photos.isNotEmpty ? photos.first : null;

  factory VehicleSummary.fromJson(Map<String, dynamic> json) => VehicleSummary(
        id: json['id'] as String,
        make: json['make'] as String,
        model: json['model'] as String,
        year: json['year'] as int,
        licensePlate: json['licensePlate'] as String,
        photos: (json['photos'] as List<dynamic>?)
                ?.map((e) => e as String)
                .toList() ??
            [],
      );
}

class CustomerSummary {
  final String id;
  final String firstName;
  final String lastName;
  final String? email;
  final String? phone;

  const CustomerSummary({
    required this.id,
    required this.firstName,
    required this.lastName,
    this.email,
    this.phone,
  });

  String get fullName => '$firstName $lastName';

  factory CustomerSummary.fromJson(Map<String, dynamic> json) =>
      CustomerSummary(
        id: json['id'] as String,
        firstName: json['firstName'] as String,
        lastName: json['lastName'] as String,
        email: json['email'] as String?,
        phone: json['phone'] as String?,
      );
}

class ReservationListResponse {
  final List<Reservation> data;
  final int total;
  final int page;
  final int pageSize;

  const ReservationListResponse({
    required this.data,
    required this.total,
    required this.page,
    required this.pageSize,
  });

  factory ReservationListResponse.fromJson(Map<String, dynamic> json) =>
      ReservationListResponse(
        data: (json['data'] as List<dynamic>)
            .map((e) => Reservation.fromJson(e as Map<String, dynamic>))
            .toList(),
        total: json['total'] as int? ?? 0,
        page: json['page'] as int? ?? 1,
        pageSize: json['pageSize'] as int? ?? 20,
      );
}
