class RouteStopModel {
  final String id;
  final String routeId;
  final String stopId;
  final int stopOrder;
  final int? travelTimeMinutes;
  final bool isPickupPoint;
  final bool isDropoffPoint;
  final DateTime createdAt;

  // Populated from joined data
  final String? stopName;
  final double? latitude;
  final double? longitude;
  final String? city;

  RouteStopModel({
    required this.id,
    required this.routeId,
    required this.stopId,
    required this.stopOrder,
    this.travelTimeMinutes,
    required this.isPickupPoint,
    required this.isDropoffPoint,
    required this.createdAt,
    this.stopName,
    this.latitude,
    this.longitude,
    this.city,
  });

  factory RouteStopModel.fromJson(Map<String, dynamic> json) {
    return RouteStopModel(
      id: json['id'] as String,
      routeId: json['route_id'] as String,
      stopId: json['stop_id'] as String,
      stopOrder: json['stop_order'] as int,
      travelTimeMinutes: json['travel_time_minutes'] as int?,
      isPickupPoint: json['is_pickup_point'] as bool,
      isDropoffPoint: json['is_dropoff_point'] as bool,
      createdAt: DateTime.parse(json['created_at'] as String),
      stopName: json['stop_name'] as String?,
      latitude: json['latitude']?.toDouble(),
      longitude: json['longitude']?.toDouble(),
      city: json['city'] as String?,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'route_id': routeId,
      'stop_id': stopId,
      'stop_order': stopOrder,
      'travel_time_minutes': travelTimeMinutes,
      'is_pickup_point': isPickupPoint,
      'is_dropoff_point': isDropoffPoint,
      'created_at': createdAt.toIso8601String(),
      if (stopName != null) 'stop_name': stopName,
      if (latitude != null) 'latitude': latitude,
      if (longitude != null) 'longitude': longitude,
      if (city != null) 'city': city,
    };
  }

  String get operationType {
    if (isPickupPoint && isDropoffPoint) return 'Subida/Bajada';
    if (isPickupPoint) return 'Solo Subida';
    if (isDropoffPoint) return 'Solo Bajada';
    return 'Sin servicio';
  }
}
