import 'package:flutter/material.dart'; import '../models/notificacion.dart'; import '../widgets/notification_banner.dart'; class NotificationService extends ChangeNotifier { final GlobalKey navigatorKey; final List _notifications = []; NotificationService({required this.navigatorKey}); List get notifications => List.unmodifiable(_notifications); int get unreadCount => _notifications.where((n) => !n.leida).length; void mostrar(Notificacion notif) { _notifications.insert(0, notif); notifyListeners(); _showBanner(notif); } void marcarTodasLeidas() { for (final n in _notifications) { n.leida = true; } notifyListeners(); } void marcarLeida(String id) { final idx = _notifications.indexWhere((n) => n.id == id); if (idx >= 0) { _notifications[idx].leida = true; notifyListeners(); } } void eliminar(String id) { _notifications.removeWhere((n) => n.id == id); notifyListeners(); } void _showBanner(Notificacion notif) { final overlay = navigatorKey.currentState?.overlay; if (overlay == null) return; OverlayEntry? entry; entry = OverlayEntry( builder: (_) => NotificationBanner( notificacion: notif, onDismiss: () => entry?.remove(), ), ); overlay.insert(entry); } // ── Disparadores de conveniencia ───────────────────────────── void notifPedidoConfirmado(String id, String cliente) { mostrar(Notificacion( id: '${DateTime.now().millisecondsSinceEpoch}', tipo: TipoNotificacion.pedidoConfirmado, titulo: 'Pedido confirmado', cuerpo: '$id firmado y confirmado — $cliente', fecha: DateTime.now(), )); } void notifParadaVisitada(String cliente) { mostrar(Notificacion( id: '${DateTime.now().millisecondsSinceEpoch}', tipo: TipoNotificacion.pedidoEntregado, titulo: 'Visita completada', cuerpo: 'Parada en $cliente marcada como visitada', fecha: DateTime.now(), )); } void notifStockBajo(String producto, int stock) { mostrar(Notificacion( id: '${DateTime.now().millisecondsSinceEpoch}', tipo: TipoNotificacion.stockBajo, titulo: 'Stock bajo', cuerpo: '$producto — solo $stock unidades disponibles', fecha: DateTime.now(), )); } Future dispararDemo() async { final base = DateTime.now().millisecondsSinceEpoch; final demos = [ Notificacion( id: '${base}_1', tipo: TipoNotificacion.paradaProxima, titulo: 'Próxima parada en 15 min', cuerpo: 'InfoSystems — Av. Naciones Unidas, Quito', fecha: DateTime.now(), ), Notificacion( id: '${base}_2', tipo: TipoNotificacion.stockBajo, titulo: 'Stock bajo', cuerpo: 'Router TP-Link AX3000 — solo 3 unidades disponibles', fecha: DateTime.now(), ), Notificacion( id: '${base}_3', tipo: TipoNotificacion.pedidoConfirmado, titulo: 'Pedido confirmado', cuerpo: 'TTC-0047 firmado por TechZone — \$24,500.00', fecha: DateTime.now(), ), Notificacion( id: '${base}_4', tipo: TipoNotificacion.metaVentas, titulo: '¡Meta del día alcanzada!', cuerpo: 'Superaste \$15,000 en ventas. ¡Excelente trabajo!', fecha: DateTime.now(), ), Notificacion( id: '${base}_5', tipo: TipoNotificacion.recordatorio, titulo: 'Recordatorio de visita', cuerpo: 'Gobernación de Pichincha — contrato de renovación anual pendiente', fecha: DateTime.now(), ), ]; for (final n in demos) { await Future.delayed(const Duration(milliseconds: 950)); mostrar(n); } } }