class RouteModel {
  final String id;
  final String name;
  final String? description;
  final String color;
  final String direction;
  final String? originCity;
  final String? destinationCity;
  final double? distanceKm;
  final int? estimatedDurationMinutes;
  final String status;
  final DateTime? createdAt;
  final DateTime? updatedAt;

  RouteModel({
    required this.id,
    required this.name,
    this.description,
    required this.color,
    required this.direction,
    this.originCity,
    this.destinationCity,
    this.distanceKm,
    this.estimatedDurationMinutes,
    this.status = 'active',
    this.createdAt,
    this.updatedAt,
  });

  factory RouteModel.fromJson(Map<String, dynamic> json) {
    return RouteModel(
      id: json['id']?.toString() ?? '',
      name: json['name']?.toString() ?? '',
      description: json['description']?.toString(),
      color: json['color']?.toString() ?? '#FEE715',
      direction: json['direction']?.toString() ?? 'outbound',
      originCity: json['origin_city']?.toString(),
      destinationCity: json['destination_city']?.toString(),
      distanceKm: json['distance_km'] != null
          ? double.tryParse(json['distance_km'].toString())
          : null,
      estimatedDurationMinutes: json['estimated_duration_minutes'] != null
          ? int.tryParse(json['estimated_duration_minutes'].toString())
          : null,
      status: json['status']?.toString() ?? 'active',
      createdAt: json['created_at'] != null
          ? DateTime.tryParse(json['created_at'].toString())
          : null,
      updatedAt: json['updated_at'] != null
          ? DateTime.tryParse(json['updated_at'].toString())
          : null,
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'name': name,
      'description': description,
      'color': color,
      'direction': direction,
      'origin_city': originCity,
      'destination_city': destinationCity,
      'distance_km': distanceKm,
      'estimated_duration_minutes': estimatedDurationMinutes,
      'status': status,
      'created_at': createdAt?.toIso8601String(),
      'updated_at': updatedAt?.toIso8601String(),
    };
  }

  // Helper getters
  String get displayName {
    if (name.isNotEmpty) return name;
    final od = [originCity, destinationCity]
        .where((e) => e != null && e.trim().isNotEmpty)
        .map((e) => e!.trim())
        .join(' – ');
    return od.isNotEmpty ? od : 'Route';
  }

  String get routeDescription {
    if (description != null && description!.isNotEmpty) {
      return description!;
    }
    return 'Ruta $displayName';
  }

  String get durationText {
    if (estimatedDurationMinutes != null) {
      if (estimatedDurationMinutes! >= 60) {
        final hours = estimatedDurationMinutes! ~/ 60;
        final minutes = estimatedDurationMinutes! % 60;
        return minutes > 0 ? '${hours}h ${minutes}min' : '${hours}h';
      }
      return '${estimatedDurationMinutes}min';
    }
    return 'N/A';
  }

  String get distanceText {
    if (distanceKm != null) {
      return '${distanceKm!.toStringAsFixed(1)} km';
    }
    return 'N/A';
  }

  bool get isActive => status == 'active';

  RouteModel copyWith({
    String? id,
    String? name,
    String? description,
    String? color,
    String? direction,
    String? originCity,
    String? destinationCity,
    double? distanceKm,
    int? estimatedDurationMinutes,
    String? status,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return RouteModel(
      id: id ?? this.id,
      name: name ?? this.name,
      description: description ?? this.description,
      color: color ?? this.color,
      direction: direction ?? this.direction,
      originCity: originCity ?? this.originCity,
      destinationCity: destinationCity ?? this.destinationCity,
      distanceKm: distanceKm ?? this.distanceKm,
      estimatedDurationMinutes:
          estimatedDurationMinutes ?? this.estimatedDurationMinutes,
      status: status ?? this.status,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  @override
  String toString() {
    return 'RouteModel(id: $id, name: $name, direction: $direction, status: $status)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is RouteModel && other.id == id;
  }

  @override
  int get hashCode => id.hashCode;
}
