class DashboardMetrics { final DashboardKpis kpis; final List recentReservations; final List sourceBreakdown; final DashboardSubscription? subscription; const DashboardMetrics({ required this.kpis, required this.recentReservations, required this.sourceBreakdown, required this.subscription, }); factory DashboardMetrics.fromJson(Map json) { final kpiJson = json['kpis'] as Map?; return DashboardMetrics( kpis: DashboardKpis.fromJson(kpiJson ?? json), recentReservations: (json['recentReservations'] as List?) ?.map( (e) => DashboardRecentReservation.fromJson( e as Map, ), ) .toList() ?? const [], sourceBreakdown: (json['sourceBreakdown'] as List?) ?.map( (e) => DashboardSourceBreakdown.fromJson( e as Map, ), ) .toList() ?? const [], subscription: json['subscription'] is Map ? DashboardSubscription.fromJson( json['subscription'] as Map, ) : null, ); } factory DashboardMetrics.empty() => DashboardMetrics( kpis: DashboardKpis.empty(), recentReservations: const [], sourceBreakdown: const [], subscription: null, ); } class DashboardKpis { final int totalBookings; final int activeVehicles; final double monthlyRevenue; final int totalCustomers; final double bookingsChange; final double vehiclesChange; final double revenueChange; final double customersChange; const DashboardKpis({ required this.totalBookings, required this.activeVehicles, required this.monthlyRevenue, required this.totalCustomers, required this.bookingsChange, required this.vehiclesChange, required this.revenueChange, required this.customersChange, }); factory DashboardKpis.fromJson(Map json) => DashboardKpis( totalBookings: (json['totalBookings'] as num?)?.toInt() ?? (json['totalReservations'] as num?)?.toInt() ?? 0, activeVehicles: (json['activeVehicles'] as num?)?.toInt() ?? (json['availableVehicles'] as num?)?.toInt() ?? (json['totalVehicles'] as num?)?.toInt() ?? 0, monthlyRevenue: (json['monthlyRevenue'] as num?)?.toDouble() ?? (json['totalRevenue'] as num?)?.toDouble() ?? 0, totalCustomers: (json['totalCustomers'] as num?)?.toInt() ?? 0, bookingsChange: (json['bookingsChange'] as num?)?.toDouble() ?? 0, vehiclesChange: (json['vehiclesChange'] as num?)?.toDouble() ?? 0, revenueChange: (json['revenueChange'] as num?)?.toDouble() ?? 0, customersChange: (json['customersChange'] as num?)?.toDouble() ?? 0, ); factory DashboardKpis.empty() => const DashboardKpis( totalBookings: 0, activeVehicles: 0, monthlyRevenue: 0, totalCustomers: 0, bookingsChange: 0, vehiclesChange: 0, revenueChange: 0, customersChange: 0, ); } class DashboardRecentReservation { final String id; final String bookingRef; final String customerName; final String vehicleName; final DateTime startDate; final DateTime endDate; final String status; final double totalAmount; const DashboardRecentReservation({ required this.id, required this.bookingRef, required this.customerName, required this.vehicleName, required this.startDate, required this.endDate, required this.status, required this.totalAmount, }); factory DashboardRecentReservation.fromJson(Map json) => DashboardRecentReservation( id: json['id'] as String, bookingRef: json['bookingRef'] as String? ?? '', customerName: json['customerName'] as String? ?? 'Unknown customer', vehicleName: json['vehicleName'] as String? ?? 'Unknown vehicle', startDate: DateTime.tryParse(json['startDate'] as String? ?? '') ?? DateTime.now(), endDate: DateTime.tryParse(json['endDate'] as String? ?? '') ?? DateTime.now(), status: json['status'] as String? ?? 'UNKNOWN', totalAmount: (json['totalAmount'] as num?)?.toDouble() ?? 0, ); } class DashboardSourceBreakdown { final String source; final int count; final double revenue; const DashboardSourceBreakdown({ required this.source, required this.count, required this.revenue, }); factory DashboardSourceBreakdown.fromJson(Map json) => DashboardSourceBreakdown( source: json['source'] as String? ?? 'UNKNOWN', count: (json['count'] as num?)?.toInt() ?? 0, revenue: (json['revenue'] as num?)?.toDouble() ?? 0, ); } class DashboardSubscription { final String status; final String? planName; final DateTime? trialEndsAt; const DashboardSubscription({ required this.status, required this.planName, required this.trialEndsAt, }); factory DashboardSubscription.fromJson(Map json) => DashboardSubscription( status: json['status'] as String? ?? 'ACTIVE', planName: json['planName'] as String?, trialEndsAt: json['trialEndsAt'] == null ? null : DateTime.tryParse(json['trialEndsAt'] as String), ); }