// ABOUTME: Export options model for specifying user preferences for data export
// ABOUTME: Controls data type selection, date ranges, and format for data exports
import 'package:equatable/equatable.dart';

/// Enumeration of data types that can be exported
enum ExportDataType {
  all,
  weight,
  bloodPressure,
  mood,
}

/// Enumeration of export formats
enum ExportFormat {
  json, // Standard format for backup/restore
  csv,  // For external analysis
}

/// Enumeration of export locations
enum ExportLocation {
  appDirectory, // Internal app directory
  saveDialog,   // Show system save dialog
}

/// Model for user-selected export options
class ExportOptions extends Equatable {
  final Set<ExportDataType> dataTypes;
  final DateTime? startDate;
  final DateTime? endDate;
  final ExportFormat format;
  final bool includeSettings;
  final bool encryptExport;
  final ExportLocation location;

  const ExportOptions({
    this.dataTypes = const {ExportDataType.all},
    this.startDate,
    this.endDate,
    this.format = ExportFormat.json,
    this.includeSettings = false,
    this.encryptExport = false,
    this.location = ExportLocation.appDirectory,
  });

  /// Default options for a full backup
  factory ExportOptions.fullBackup() {
    return const ExportOptions(
      dataTypes: {ExportDataType.all},
      format: ExportFormat.json,
      includeSettings: true,
      encryptExport: false,
    );
  }

  /// Default options for CSV export
  factory ExportOptions.csvExport() {
    return const ExportOptions(
      dataTypes: {ExportDataType.all},
      format: ExportFormat.csv,
      includeSettings: false,
      encryptExport: false,
    );
  }

  /// Check if a specific data type should be exported
  bool shouldExport(ExportDataType type) {
    return dataTypes.contains(ExportDataType.all) || dataTypes.contains(type);
  }

  /// Check if a date range is selected
  bool get hasDateRange => startDate != null && endDate != null;

  /// Get human-readable description of selected data types
  String get dataTypesDescription {
    if (dataTypes.contains(ExportDataType.all)) {
      return 'All Data';
    }
    if (dataTypes.isEmpty) {
      return 'None Selected';
    }
    return dataTypes.map((t) => t.name).join(', ');
  }

  /// Get human-readable description of date range
  String get dateRangeDescription {
    if (startDate == null || endDate == null) {
      return 'All Time';
    }
    return '${_formatDate(startDate!)} - ${_formatDate(endDate!)}';
  }

  String _formatDate(DateTime date) {
    return '${date.year}-${date.month.toString().padLeft(2, '0')}-${date.day.toString().padLeft(2, '0')}';
  }

  /// Create copy with updated fields
  ExportOptions copyWith({
    Set<ExportDataType>? dataTypes,
    DateTime? startDate,
    DateTime? endDate,
    ExportFormat? format,
    bool? includeSettings,
    bool? encryptExport,
    ExportLocation? location,
  }) {
    return ExportOptions(
      dataTypes: dataTypes ?? this.dataTypes,
      startDate: startDate ?? this.startDate,
      endDate: endDate ?? this.endDate,
      format: format ?? this.format,
      includeSettings: includeSettings ?? this.includeSettings,
      encryptExport: encryptExport ?? this.encryptExport,
      location: location ?? this.location,
    );
  }

  @override
  List<Object?> get props => [
        dataTypes,
        startDate,
        endDate,
        format,
        includeSettings,
        encryptExport,
        location,
      ];

  @override
  String toString() {
    return 'ExportOptions(types: $dataTypesDescription, range: $dateRangeDescription, format: $format)';
  }
}
