import '../api/types.dart';

class DeliveryRoute implements Serializable {
  final int id;
  final int routeId;
  final String name;
  final String routeName;
  final int deliveriesCount;
  final int deliveredCount;
  final bool completed;
  final String createdAt;

  const DeliveryRoute({
    required this.id,
    required this.routeId,
    required this.name,
    required this.routeName,
    required this.deliveriesCount,
    required this.deliveredCount,
    required this.completed,
    required this.createdAt,
  });

  factory DeliveryRoute.fromJson(Map<String, dynamic> json) {
    return DeliveryRoute(
      id: json['id'] as int,
      routeId: json['routeId'] as int,
      name: json['name'] as String,
      routeName: json['routeName'] as String,
      deliveriesCount: json['deliveriesCount'] as int,
      deliveredCount: json['deliveredCount'] as int,
      completed: json['completed'] as bool,
      createdAt: json['createdAt'] as String,
    );
  }

  double get progress {
    if (deliveriesCount == 0) return 0.0;
    return deliveredCount / deliveriesCount;
  }

  int get pendingDeliveries => deliveriesCount - deliveredCount;

  @override
  Map<String, Object?> toJson() => {
    'id': id,
    'routeId': routeId,
    'name': name,
    'routeName': routeName,
    'deliveriesCount': deliveriesCount,
    'deliveredCount': deliveredCount,
    'completed': completed,
    'createdAt': createdAt,
  };
}
