// ABOUTME: Main application settings model containing all user preferences
// ABOUTME: Aggregates theme, unit, and privacy settings into a single model with JSON serialization
import 'package:equatable/equatable.dart';
import 'theme_settings.dart';
import 'unit_settings.dart';
import 'privacy_settings.dart';

/// Main application settings model
/// Contains all user preferences and settings for the Health Tracker app
class AppSettings extends Equatable {
  final ThemeSettings themeSettings;
  final UnitSettings unitSettings;
  final PrivacySettings privacySettings;
  final String language;
  final bool enableNotifications;
  final bool enableAnalytics;

  const AppSettings({
    required this.themeSettings,
    required this.unitSettings,
    required this.privacySettings,
    this.language = 'en',
    this.enableNotifications = true,
    this.enableAnalytics = false, // Privacy-first approach
  });

  /// Default settings for first-time users
  factory AppSettings.defaults() {
    return AppSettings(
      themeSettings: ThemeSettings.defaults(),
      unitSettings: UnitSettings.defaults(),
      privacySettings: PrivacySettings.defaults(),
    );
  }

  /// Create AppSettings from JSON map
  factory AppSettings.fromJson(Map<String, dynamic> json) {
    return AppSettings(
      themeSettings: ThemeSettings.fromJson(json['themeSettings'] ?? {}),
      unitSettings: UnitSettings.fromJson(json['unitSettings'] ?? {}),
      privacySettings: PrivacySettings.fromJson(json['privacySettings'] ?? {}),
      language: json['language'] ?? 'en',
      enableNotifications: json['enableNotifications'] ?? true,
      enableAnalytics: json['enableAnalytics'] ?? false,
    );
  }

  /// Convert AppSettings to JSON map
  Map<String, dynamic> toJson() {
    return {
      'themeSettings': themeSettings.toJson(),
      'unitSettings': unitSettings.toJson(),
      'privacySettings': privacySettings.toJson(),
      'language': language,
      'enableNotifications': enableNotifications,
      'enableAnalytics': enableAnalytics,
    };
  }

  /// Create copy with updated fields
  AppSettings copyWith({
    ThemeSettings? themeSettings,
    UnitSettings? unitSettings,
    PrivacySettings? privacySettings,
    String? language,
    bool? enableNotifications,
    bool? enableAnalytics,
  }) {
    return AppSettings(
      themeSettings: themeSettings ?? this.themeSettings,
      unitSettings: unitSettings ?? this.unitSettings,
      privacySettings: privacySettings ?? this.privacySettings,
      language: language ?? this.language,
      enableNotifications: enableNotifications ?? this.enableNotifications,
      enableAnalytics: enableAnalytics ?? this.enableAnalytics,
    );
  }

  @override
  List<Object?> get props => [
        themeSettings,
        unitSettings,
        privacySettings,
        language,
        enableNotifications,
        enableAnalytics,
      ];

  @override
  String toString() {
    return 'AppSettings(theme: $themeSettings, units: $unitSettings, privacy: $privacySettings, language: $language, notifications: $enableNotifications, analytics: $enableAnalytics)';
  }
}
