import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../providers/user_provider.dart';
import '../../services/pdf_service.dart';
import '../../services/ics_service.dart';

class ExportDataScreen extends ConsumerWidget {
  const ExportDataScreen({super.key});

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final userProfile = ref.watch(userProfileProvider);
    final cycleEntries = ref.watch(cycleEntriesProvider);

    return Scaffold(
      appBar: AppBar(
        title: const Text('Export Data'),
      ),
      body: userProfile == null
          ? const Center(child: CircularProgressIndicator())
          : ListView(
              padding: const EdgeInsets.all(16.0),
              children: [
                ListTile(
                  leading: const Icon(Icons.picture_as_pdf),
                  title: const Text('Export as PDF'),
                  subtitle: const Text(
                      'Generate a printable PDF report of your cycle data.'),
                  trailing: const Icon(Icons.chevron_right),
                  onTap: () async {
                    try {
                      if (context.mounted) {
                        ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(
                              content: Text('Generating PDF report...')),
                        );
                      }
                      await PdfService.generateCycleReport(
                          userProfile, cycleEntries);
                      if (context.mounted) {
                        ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(
                              content:
                                  Text('PDF report generated successfully!')),
                        );
                      }
                    } catch (e) {
                      if (context.mounted) {
                        ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(content: Text('Failed to generate PDF: $e')),
                        );
                      }
                    }
                  },
                ),
                ListTile(
                  leading: const Icon(Icons.sync),
                  title: const Text('Sync with Calendar'),
                  subtitle: const Text(
                      'Export to Apple, Google, or Outlook Calendar.'),
                  trailing: const Icon(Icons.chevron_right),
                  onTap: () async {
                    // Show options dialog
                    final includePredictions = await showDialog<bool>(
                      context: context,
                      builder: (context) => AlertDialog(
                        title: const Text('Calendar Sync Options'),
                        content: const Text(
                            'Would you like to include predicted future periods for the next 12 months?'),
                        actions: [
                          TextButton(
                            onPressed: () => Navigator.pop(context, false),
                            child: const Text('No, only history'),
                          ),
                          TextButton(
                            onPressed: () => Navigator.pop(context, true),
                            child: const Text('Yes, include predictions'),
                          ),
                        ],
                      ),
                    );

                    if (includePredictions == null) {
                      return; // User cancelled dialog
                    }

                    try {
                      if (context.mounted) {
                        ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(
                              content: Text('Generating calendar file...')),
                        );
                      }

                      await IcsService.generateCycleCalendar(cycleEntries,
                          user: userProfile,
                          includePredictions: includePredictions);

                      if (context.mounted) {
                        ScaffoldMessenger.of(context).showSnackBar(
                          const SnackBar(
                              content: Text(
                                  'Calendar file generated! Open it to add to your calendar.')),
                        );
                      }
                    } catch (e) {
                      if (context.mounted) {
                        ScaffoldMessenger.of(context).showSnackBar(
                          SnackBar(
                              content:
                                  Text('Failed to generate calendar file: $e')),
                        );
                      }
                    }
                  },
                ),
              ],
            ),
    );
  }
}
