// ABOUTME: Export dialog widget for configuring backup export options with data type and range selection
// ABOUTME: Provides user-friendly interface for customizing export settings before creating backup files
import 'package:flutter/material.dart';
import '../../models/backup/export_options.dart';

/// Dialog for configuring export options
class ExportDialog extends StatefulWidget {
  final ExportOptions initialOptions;
  final VoidCallback? onExport;

  const ExportDialog({
    super.key,
    required this.initialOptions,
    this.onExport,
  });

  @override
  State<ExportDialog> createState() => _ExportDialogState();
}

class _ExportDialogState extends State<ExportDialog> {
  late ExportOptions _options;

  @override
  void initState() {
    super.initState();
    _options = widget.initialOptions;
  }

  @override
  Widget build(BuildContext context) {
    return AlertDialog(
      title: const Text('Export Data'),
      content: SizedBox(
        width: double.maxFinite,
        child: Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              'Choose what data to export:',
              style: Theme.of(context).textTheme.titleSmall,
            ),
            const SizedBox(height: 16),
            
            // Data type selection
            _buildDataTypeSelection(),
            
            const SizedBox(height: 24),
            
            // Format selection
            Text(
              'Export format:',
              style: Theme.of(context).textTheme.titleSmall,
            ),
            const SizedBox(height: 8),
            _buildFormatSelection(),
            
            const SizedBox(height: 24),
            
            // Additional options
            Text(
              'Additional options:',
              style: Theme.of(context).textTheme.titleSmall,
            ),
            const SizedBox(height: 8),
            _buildAdditionalOptions(),
          ],
        ),
      ),
      actions: [
        TextButton(
          onPressed: () => Navigator.of(context).pop(),
          child: const Text('Cancel'),
        ),
        FilledButton(
          onPressed: () {
            Navigator.of(context).pop(_options);
            widget.onExport?.call();
          },
          child: const Text('Export'),
        ),
      ],
    );
  }

  Widget _buildDataTypeSelection() {
    return Column(
      children: [
        CheckboxListTile(
          title: const Text('All Data'),
          value: _options.dataTypes.contains(ExportDataType.all),
          onChanged: (value) {
            setState(() {
              if (value == true) {
                _options = _options.copyWith(dataTypes: {ExportDataType.all});
              } else {
                _options = _options.copyWith(dataTypes: {});
              }
            });
          },
        ),
        if (!_options.dataTypes.contains(ExportDataType.all)) ...[
          CheckboxListTile(
            title: const Text('Weight Data'),
            value: _options.dataTypes.contains(ExportDataType.weight),
            onChanged: (value) {
              setState(() {
                final types = Set<ExportDataType>.from(_options.dataTypes);
                if (value == true) {
                  types.add(ExportDataType.weight);
                } else {
                  types.remove(ExportDataType.weight);
                }
                _options = _options.copyWith(dataTypes: types);
              });
            },
          ),
          CheckboxListTile(
            title: const Text('Blood Pressure Data'),
            value: _options.dataTypes.contains(ExportDataType.bloodPressure),
            onChanged: (value) {
              setState(() {
                final types = Set<ExportDataType>.from(_options.dataTypes);
                if (value == true) {
                  types.add(ExportDataType.bloodPressure);
                } else {
                  types.remove(ExportDataType.bloodPressure);
                }
                _options = _options.copyWith(dataTypes: types);
              });
            },
          ),
          CheckboxListTile(
            title: const Text('Mood Data'),
            value: _options.dataTypes.contains(ExportDataType.mood),
            onChanged: (value) {
              setState(() {
                final types = Set<ExportDataType>.from(_options.dataTypes);
                if (value == true) {
                  types.add(ExportDataType.mood);
                } else {
                  types.remove(ExportDataType.mood);
                }
                _options = _options.copyWith(dataTypes: types);
              });
            },
          ),
        ],
      ],
    );
  }

  Widget _buildFormatSelection() {
    return SegmentedButton<ExportFormat>(
      segments: const [
        ButtonSegment<ExportFormat>(
          value: ExportFormat.json,
          label: Text('Backup'),
          icon: Icon(Icons.backup),
        ),
        ButtonSegment<ExportFormat>(
          value: ExportFormat.csv,
          label: Text('CSV'),
          icon: Icon(Icons.table_chart),
        ),
      ],
      selected: {_options.format},
      onSelectionChanged: (Set<ExportFormat> selection) {
        setState(() {
          _options = _options.copyWith(format: selection.first);
        });
      },
    );
  }

  Widget _buildAdditionalOptions() {
    return Column(
      children: [
        if (_options.format == ExportFormat.json)
          SwitchListTile(
            title: const Text('Include App Settings'),
            subtitle: const Text('Export app preferences and configuration'),
            value: _options.includeSettings,
            onChanged: (value) {
              setState(() {
                _options = _options.copyWith(includeSettings: value);
              });
            },
          ),
        if (_options.format == ExportFormat.csv)
          SwitchListTile(
            title: const Text('Choose Save Location'),
            subtitle: const Text('Show save dialog to choose where to save the CSV file'),
            value: _options.location == ExportLocation.saveDialog,
            onChanged: (value) {
              setState(() {
                _options = _options.copyWith(
                  location: value ? ExportLocation.saveDialog : ExportLocation.appDirectory,
                );
              });
            },
          ),
      ],
    );
  }
}

/// Show export dialog and return selected options
Future<ExportOptions?> showExportDialog({
  required BuildContext context,
  ExportOptions? initialOptions,
  VoidCallback? onExport,
}) async {
  return showDialog<ExportOptions>(
    context: context,
    builder: (context) => ExportDialog(
      initialOptions: initialOptions ?? ExportOptions.fullBackup(),
      onExport: onExport,
    ),
  );
}
