import 'dart:convert'; import 'package:flutter/services.dart' show rootBundle; import '../models/colony_route.dart'; import '../models/calendar_event_entry.dart'; import '../models/demo_profile.dart'; import '../models/route_guide_entry.dart'; import '../models/route_notification.dart'; import '../models/truck_route.dart'; class LocalSeedRepository { LocalSeedRepository._(); static final LocalSeedRepository instance = LocalSeedRepository._(); Future? _cachedLoad; Future load() { return _cachedLoad ??= _loadInternal(); } Future _loadInternal() async { try { final routesJson = await rootBundle.loadString('assets/json/rutas.json'); final notificationsJson = await rootBundle.loadString('assets/json/notificaciones.json'); final colonyRoutesJson = await rootBundle.loadString('assets/json/colonias-rutas.json'); final profilesJson = await rootBundle.loadString('assets/json/perfiles.json'); final calendarEventsJson = await rootBundle.loadString('assets/json/calendario.json'); final routeGuidesJson = await rootBundle.loadString('assets/json/guia-rutas.json'); final routes = _decodeList(routesJson).map(TruckRoute.fromJson).toList(growable: false); final notifications = _decodeList(notificationsJson).map(RouteNotification.fromJson).toList(growable: false); final colonyRoutes = _decodeList(colonyRoutesJson).map(ColonyRoute.fromJson).toList(growable: false); final profiles = _decodeList(profilesJson).map(DemoProfile.fromJson).toList(growable: false); final calendarEvents = _decodeList(calendarEventsJson).map(CalendarEventEntry.fromJson).toList(growable: false); final routeGuides = _decodeList(routeGuidesJson).map(RouteGuideEntry.fromJson).toList(growable: false); return LocalSeedData( routes: routes, notifications: notifications, colonyRoutes: colonyRoutes, demoProfiles: profiles, calendarEvents: calendarEvents, routeGuides: routeGuides, ); } catch (_) { return LocalSeedData.empty(); } } List> _decodeList(String responseBody) { final decoded = jsonDecode(responseBody); if (decoded is! List) { return >[]; } return decoded.whereType>().toList(growable: false); } } class LocalSeedData { const LocalSeedData({ required this.routes, required this.notifications, required this.colonyRoutes, required this.demoProfiles, required this.calendarEvents, required this.routeGuides, }); final List routes; final List notifications; final List colonyRoutes; final List demoProfiles; final List calendarEvents; final List routeGuides; const LocalSeedData.empty() : routes = const [], notifications = const [], colonyRoutes = const [], demoProfiles = const [], calendarEvents = const [], routeGuides = const []; TruckRoute? get defaultRoute => routes.isEmpty ? null : routes.first; TruckRoute? routeForColonia(String? colonia) { if (colonia == null || colonia.trim().isEmpty) { return defaultRoute; } final routeId = colonyRouteForColonia(colonia)?.routeId; if (routeId == null) { return defaultRoute; } return routeById(routeId) ?? defaultRoute; } ColonyRoute? colonyRouteForColonia(String colonia) { final normalized = colonia.trim().toLowerCase(); for (final item in colonyRoutes) { if (item.colonia.trim().toLowerCase() == normalized) { return item; } } return null; } TruckRoute? routeById(String routeId) { for (final route in routes) { if (route.routeId == routeId) { return route; } } return null; } DemoProfile? profileForCredentials(String email, String password) { final normalizedEmail = email.trim().toLowerCase(); for (final profile in demoProfiles) { if (profile.email.trim().toLowerCase() == normalizedEmail && profile.password == password) { return profile; } } return null; } DemoProfile? profileForColonia(String? colonia) { if (colonia == null || colonia.trim().isEmpty) { return demoProfiles.isEmpty ? null : demoProfiles.first; } final normalized = colonia.trim().toLowerCase(); for (final profile in demoProfiles) { if (profile.colonia.trim().toLowerCase() == normalized) { return profile; } } return demoProfiles.isEmpty ? null : demoProfiles.first; } List eventsForDay(DateTime day) { final normalized = DateTime(day.year, day.month, day.day); return calendarEvents .where((event) => event.date.year == normalized.year && event.date.month == normalized.month && event.date.day == normalized.day) .toList(growable: false); } RouteGuideEntry? guideForRouteId(String routeId) { for (final guide in routeGuides) { if (guide.routeId == routeId) { return guide; } } return null; } }