import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:intl/intl.dart'; import 'package:collection/collection.dart'; import '../../models/cycle_entry.dart'; import '../../providers/user_provider.dart'; class CycleHistoryScreen extends ConsumerWidget { const CycleHistoryScreen({super.key}); void _showDeleteAllDialog(BuildContext context, WidgetRef ref) { showDialog( context: context, builder: (context) => AlertDialog( title: const Text('Delete All History?'), content: const Text( 'This will permanently delete all cycle entries. This action cannot be undone.'), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: const Text('Cancel'), ), TextButton( onPressed: () { ref.read(cycleEntriesProvider.notifier).clearEntries(); Navigator.pop(context); ScaffoldMessenger.of(context).showSnackBar( const SnackBar(content: Text('All cycle history has been deleted.')), ); }, child: const Text('Delete All', style: TextStyle(color: Colors.red)), ), ], ), ); } void _showDeleteMonthDialog(BuildContext context, WidgetRef ref) { showDialog( context: context, builder: (context) => const _DeleteMonthDialog(), ); } @override Widget build(BuildContext context, WidgetRef ref) { final entries = ref.watch(cycleEntriesProvider); final groupedEntries = groupBy( entries, (CycleEntry entry) => DateFormat('MMMM yyyy').format(entry.date), ); return Scaffold( appBar: AppBar( title: const Text('Cycle History'), actions: [ if (entries.isNotEmpty) PopupMenuButton( onSelected: (value) { if (value == 'delete_all') { _showDeleteAllDialog(context, ref); } else if (value == 'delete_month') { _showDeleteMonthDialog(context, ref); } }, itemBuilder: (BuildContext context) => >[ const PopupMenuItem( value: 'delete_month', child: Text('Delete by Month'), ), const PopupMenuItem( value: 'delete_all', child: Text('Delete All Data'), ), ], ), ], ), body: entries.isEmpty ? Center( child: Text( 'No cycle history found.', style: Theme.of(context).textTheme.bodyLarge, ), ) : ListView.builder( itemCount: groupedEntries.keys.length, itemBuilder: (context, index) { final month = groupedEntries.keys.elementAt(index); final monthEntries = groupedEntries[month]!; return ExpansionTile( title: Text(month, style: Theme.of(context).textTheme.titleLarge), initiallyExpanded: index == 0, children: monthEntries.map((entry) { return Dismissible( key: Key(entry.id), direction: DismissDirection.endToStart, onDismissed: (direction) { ref.read(cycleEntriesProvider.notifier).deleteEntry(entry.id); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( 'Entry for ${DateFormat.yMMMd().format(entry.date)} deleted.')), ); }, background: Container( color: Colors.red, alignment: Alignment.centerRight, padding: const EdgeInsets.symmetric(horizontal: 20.0), child: const Icon(Icons.delete, color: Colors.white), ), child: ListTile( title: Text(DateFormat.yMMMMEEEEd().format(entry.date)), subtitle: Text(_buildEntrySummary(entry)), isThreeLine: true, ), ); }).toList(), ); }, ), ); } String _buildEntrySummary(CycleEntry entry) { final summary = []; if (entry.isPeriodDay) { summary.add('Period'); } if (entry.mood != null) { summary.add('Mood: ${entry.mood!.label}'); } if (entry.symptomCount > 0) { summary.add('${entry.symptomCount} symptom(s)'); } if (entry.notes != null && entry.notes!.isNotEmpty) { summary.add('Note'); } if (summary.isEmpty) { return 'No specific data logged.'; } return summary.join(' • '); } } class _DeleteMonthDialog extends ConsumerStatefulWidget { const _DeleteMonthDialog(); @override ConsumerState<_DeleteMonthDialog> createState() => _DeleteMonthDialogState(); } class _DeleteMonthDialogState extends ConsumerState<_DeleteMonthDialog> { late int _selectedYear; late int _selectedMonth; @override void initState() { super.initState(); final now = DateTime.now(); _selectedYear = now.year; _selectedMonth = now.month; } @override Widget build(BuildContext context) { final years = List.generate(5, (index) => DateTime.now().year - index); final months = List.generate(12, (index) => index + 1); return AlertDialog( title: const Text('Delete by Month'), content: Column( mainAxisSize: MainAxisSize.min, children: [ const Text('Select a month and year to delete all entries from.'), const SizedBox(height: 24), Row( mainAxisAlignment: MainAxisAlignment.center, children: [ DropdownButton( value: _selectedMonth, items: months.map((month) => DropdownMenuItem( value: month, child: Text(DateFormat('MMMM').format(DateTime(0, month))), )) .toList(), onChanged: (value) { if (value != null) { setState(() { _selectedMonth = value; }); } }, ), const SizedBox(width: 16), DropdownButton( value: _selectedYear, items: years .map((year) => DropdownMenuItem( value: year, child: Text(year.toString()), )) .toList(), onChanged: (value) { if (value != null) { setState(() { _selectedYear = value; }); } }, ), ], ), ], ), actions: [ TextButton( onPressed: () => Navigator.pop(context), child: const Text('Cancel'), ), TextButton( onPressed: () { ref .read(cycleEntriesProvider.notifier) .deleteEntriesForMonth(_selectedYear, _selectedMonth); Navigator.pop(context); ScaffoldMessenger.of(context).showSnackBar( SnackBar( content: Text( 'Deleted entries for ${DateFormat('MMMM yyyy').format(DateTime(0, _selectedMonth))}.')), ); }, child: const Text('Delete', style: TextStyle(color: Colors.red)), ), ], ); } }