// lib/features/routes/data/datasources/routes_local_datasource.dart // Lee rutas.json desde assets — funciona completamente offline. import 'dart:convert'; import 'package:flutter/services.dart'; import '../../domain/entities/route_entities.dart'; import '../../domain/entities/haversine.dart'; class RoutesLocalDatasource { static const _assetPath = 'assets/rutas.json'; List? _cache; Future> cargarRutas() async { if (_cache != null) return _cache!; final raw = await rootBundle.loadString(_assetPath); final List json = jsonDecode(raw); _cache = json.map((r) => TruckRoute.fromJson(r)).toList(); return _cache!; } /// Genera centroides de todas las rutas para búsqueda espacial con Haversine. Future> generarCentroides() async { final rutas = await cargarRutas(); return rutas.map(_calcularCentroide).toList(); } RouteCentroid _calcularCentroide(TruckRoute ruta) { // Excluye la última posición (regreso al depósito) final posiciones = ruta.positions.sublist(0, ruta.positions.length - 1); final avgLat = posiciones.map((p) => p.lat).reduce((a, b) => a + b) / posiciones.length; final avgLng = posiciones.map((p) => p.lng).reduce((a, b) => a + b) / posiciones.length; return RouteCentroid( routeId: ruta.routeId, routeName: ruta.name, centroidLat: avgLat, centroidLng: avgLng, ); } }