class Vehicle {
  final String id;
  final String make;
  final String model;
  final int year;
  final String licensePlate;
  final double dailyRate;
  final String status;
  final String category;
  final List<String> photos;
  final int? mileage;
  final int? seats;
  final String? transmission;
  final String? fuelType;
  final String? color;
  final bool isPublished;

  const Vehicle({
    required this.id,
    required this.make,
    required this.model,
    required this.year,
    required this.licensePlate,
    required this.dailyRate,
    required this.status,
    required this.category,
    required this.photos,
    this.mileage,
    this.seats,
    this.transmission,
    this.fuelType,
    this.color,
    required this.isPublished,
  });

  String get displayName => '$year $make $model';

  String? get primaryPhoto => photos.isNotEmpty ? photos.first : null;

  factory Vehicle.fromJson(Map<String, dynamic> json) => Vehicle(
        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,
        dailyRate: (json['dailyRate'] as num).toDouble(),
        status: json['status'] as String? ?? 'AVAILABLE',
        category: json['category'] as String? ?? 'ECONOMY',
        photos: (json['photos'] as List<dynamic>?)
                ?.map((e) => e as String)
                .toList() ??
            [],
        mileage: json['mileage'] as int?,
        seats: json['seats'] as int?,
        transmission: json['transmission'] as String?,
        fuelType: json['fuelType'] as String?,
        color: json['color'] as String?,
        isPublished: json['isPublished'] as bool? ?? false,
      );
}

class VehicleListResponse {
  final List<Vehicle> data;
  final int total;
  final int page;
  final int pageSize;

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

  factory VehicleListResponse.fromJson(Map<String, dynamic> json) =>
      VehicleListResponse(
        data: (json['data'] as List<dynamic>)
            .map((e) => Vehicle.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,
      );
}
