import 'package:flutter/foundation.dart'; import '../models/supplement.dart'; import '../models/supplement_intake.dart'; import '../services/database_helper.dart'; import '../services/notification_service.dart'; class SupplementProvider with ChangeNotifier { final DatabaseHelper _databaseHelper = DatabaseHelper.instance; final NotificationService _notificationService = NotificationService(); List _supplements = []; List> _todayIntakes = []; List> _monthlyIntakes = []; bool _isLoading = false; List get supplements => _supplements; List> get todayIntakes => _todayIntakes; List> get monthlyIntakes => _monthlyIntakes; bool get isLoading => _isLoading; Future initialize() async { await _notificationService.initialize(); await _notificationService.requestPermissions(); await loadSupplements(); await loadTodayIntakes(); } Future loadSupplements() async { _isLoading = true; notifyListeners(); try { print('Loading supplements from database...'); _supplements = await _databaseHelper.getAllSupplements(); print('Loaded ${_supplements.length} supplements'); for (var supplement in _supplements) { print('Supplement: ${supplement.name}'); } } catch (e) { print('Error loading supplements: $e'); if (kDebugMode) { print('Error loading supplements: $e'); } } finally { _isLoading = false; notifyListeners(); } } Future addSupplement(Supplement supplement) async { try { print('Adding supplement: ${supplement.name}'); final id = await _databaseHelper.insertSupplement(supplement); print('Supplement inserted with ID: $id'); final newSupplement = supplement.copyWith(id: id); // Schedule notifications (skip if there's an error) try { await _notificationService.scheduleSupplementReminders(newSupplement); print('Notifications scheduled'); } catch (notificationError) { print('Warning: Could not schedule notifications: $notificationError'); } await loadSupplements(); print('Supplements reloaded, count: ${_supplements.length}'); } catch (e) { print('Error adding supplement: $e'); if (kDebugMode) { print('Error adding supplement: $e'); } rethrow; } } Future updateSupplement(Supplement supplement) async { try { await _databaseHelper.updateSupplement(supplement); // Reschedule notifications await _notificationService.scheduleSupplementReminders(supplement); await loadSupplements(); } catch (e) { if (kDebugMode) { print('Error updating supplement: $e'); } } } Future deleteSupplement(int id) async { try { await _databaseHelper.deleteSupplement(id); // Cancel notifications await _notificationService.cancelSupplementReminders(id); await loadSupplements(); } catch (e) { if (kDebugMode) { print('Error deleting supplement: $e'); } } } Future recordIntake(int supplementId, double dosage, {int? unitsTaken, String? notes}) async { try { final intake = SupplementIntake( supplementId: supplementId, takenAt: DateTime.now(), dosageTaken: dosage, unitsTaken: unitsTaken ?? 1, notes: notes, ); await _databaseHelper.insertIntake(intake); await loadTodayIntakes(); // Show confirmation notification final supplement = _supplements.firstWhere((s) => s.id == supplementId); final unitsText = unitsTaken != null && unitsTaken > 1 ? '$unitsTaken ${supplement.unitType}' : ''; await _notificationService.showInstantNotification( 'Supplement Taken', 'Recorded ${supplement.name}${unitsText.isNotEmpty ? ' - $unitsText' : ''} ($dosage ${supplement.unit})', ); } catch (e) { if (kDebugMode) { print('Error recording intake: $e'); } } } Future loadTodayIntakes() async { try { _todayIntakes = await _databaseHelper.getIntakesWithSupplementsForDate(DateTime.now()); notifyListeners(); } catch (e) { if (kDebugMode) { print('Error loading today\'s intakes: $e'); } } } Future loadMonthlyIntakes(int year, int month) async { try { _monthlyIntakes = await _databaseHelper.getIntakesWithSupplementsForMonth(year, month); notifyListeners(); } catch (e) { if (kDebugMode) { print('Error loading monthly intakes: $e'); } } } Future>> getIntakesForDate(DateTime date) async { try { return await _databaseHelper.getIntakesWithSupplementsForDate(date); } catch (e) { if (kDebugMode) { print('Error loading intakes for date: $e'); } return []; } } Future deleteIntake(int intakeId) async { try { await _databaseHelper.deleteIntake(intakeId); await loadTodayIntakes(); } catch (e) { if (kDebugMode) { print('Error deleting intake: $e'); } } } }