// ABOUTME: Privacy settings model for managing security and data retention preferences
// ABOUTME: Controls encryption settings, data retention, backup options, and privacy controls
import 'package:equatable/equatable.dart';

/// Enumeration of data retention periods
enum DataRetentionPeriod {
  forever('Forever'),
  fiveYears('5 Years'),
  twoYears('2 Years'),
  oneYear('1 Year'),
  sixMonths('6 Months');

  const DataRetentionPeriod(this.displayName);
  final String displayName;

  /// Get retention period in days
  int get days {
    switch (this) {
      case DataRetentionPeriod.forever:
        return -1; // Special value for no deletion
      case DataRetentionPeriod.fiveYears:
        return 365 * 5;
      case DataRetentionPeriod.twoYears:
        return 365 * 2;
      case DataRetentionPeriod.oneYear:
        return 365;
      case DataRetentionPeriod.sixMonths:
        return 180;
    }
  }

  /// Create from string
  static DataRetentionPeriod fromString(String value) {
    switch (value) {
      case 'forever':
        return DataRetentionPeriod.forever;
      case 'fiveYears':
        return DataRetentionPeriod.fiveYears;
      case 'twoYears':
        return DataRetentionPeriod.twoYears;
      case 'oneYear':
        return DataRetentionPeriod.oneYear;
      case 'sixMonths':
        return DataRetentionPeriod.sixMonths;
      default:
        return DataRetentionPeriod.forever;
    }
  }

  @override
  String toString() => name;
}

/// Enumeration of backup frequency options
enum BackupFrequency {
  daily('Daily'),
  weekly('Weekly'),
  monthly('Monthly'),
  manual('Manual Only');

  const BackupFrequency(this.displayName);
  final String displayName;

  /// Create from string
  static BackupFrequency fromString(String value) {
    switch (value) {
      case 'daily':
        return BackupFrequency.daily;
      case 'weekly':
        return BackupFrequency.weekly;
      case 'monthly':
        return BackupFrequency.monthly;
      case 'manual':
        return BackupFrequency.manual;
      default:
        return BackupFrequency.manual;
    }
  }

  @override
  String toString() => name;
}

/// Privacy and security settings model
class PrivacySettings extends Equatable {
  final bool enableLocalEncryption;
  final bool requireAuthForAccess;
  final bool enableBiometric;
  final DataRetentionPeriod dataRetention;
  final BackupFrequency backupFrequency;
  final bool shareAnalytics;
  final bool shareCrashReports;
  final bool enableExportEncryption;

  const PrivacySettings({
    this.enableLocalEncryption = false,
    this.requireAuthForAccess = false,
    this.enableBiometric = false,
    this.dataRetention = DataRetentionPeriod.forever,
    this.backupFrequency = BackupFrequency.manual,
    this.shareAnalytics = false, // Privacy-first approach
    this.shareCrashReports = true, // Helps improve app
    this.enableExportEncryption = false,
  });

  /// Default privacy settings (privacy-first)
  factory PrivacySettings.defaults() {
    return const PrivacySettings(
      enableLocalEncryption: false,
      requireAuthForAccess: false,
      enableBiometric: false,
      dataRetention: DataRetentionPeriod.forever,
      backupFrequency: BackupFrequency.manual,
      shareAnalytics: false,
      shareCrashReports: true,
      enableExportEncryption: false,
    );
  }

  /// Privacy-focused defaults with maximum security
  factory PrivacySettings.secure() {
    return const PrivacySettings(
      enableLocalEncryption: true,
      requireAuthForAccess: true,
      enableBiometric: true,
      dataRetention: DataRetentionPeriod.twoYears,
      backupFrequency: BackupFrequency.manual,
      shareAnalytics: false,
      shareCrashReports: false,
      enableExportEncryption: true,
    );
  }

  /// Create PrivacySettings from JSON map
  factory PrivacySettings.fromJson(Map<String, dynamic> json) {
    return PrivacySettings(
      enableLocalEncryption: json['enableLocalEncryption'] ?? false,
      requireAuthForAccess: json['requireAuthForAccess'] ?? false,
      enableBiometric: json['enableBiometric'] ?? false,
      dataRetention: DataRetentionPeriod.fromString(json['dataRetention'] ?? 'forever'),
      backupFrequency: BackupFrequency.fromString(json['backupFrequency'] ?? 'manual'),
      shareAnalytics: json['shareAnalytics'] ?? false,
      shareCrashReports: json['shareCrashReports'] ?? true,
      enableExportEncryption: json['enableExportEncryption'] ?? false,
    );
  }

  /// Convert PrivacySettings to JSON map
  Map<String, dynamic> toJson() {
    return {
      'enableLocalEncryption': enableLocalEncryption,
      'requireAuthForAccess': requireAuthForAccess,
      'enableBiometric': enableBiometric,
      'dataRetention': dataRetention.toString(),
      'backupFrequency': backupFrequency.toString(),
      'shareAnalytics': shareAnalytics,
      'shareCrashReports': shareCrashReports,
      'enableExportEncryption': enableExportEncryption,
    };
  }

  /// Create copy with updated fields
  PrivacySettings copyWith({
    bool? enableLocalEncryption,
    bool? requireAuthForAccess,
    bool? enableBiometric,
    DataRetentionPeriod? dataRetention,
    BackupFrequency? backupFrequency,
    bool? shareAnalytics,
    bool? shareCrashReports,
    bool? enableExportEncryption,
  }) {
    return PrivacySettings(
      enableLocalEncryption: enableLocalEncryption ?? this.enableLocalEncryption,
      requireAuthForAccess: requireAuthForAccess ?? this.requireAuthForAccess,
      enableBiometric: enableBiometric ?? this.enableBiometric,
      dataRetention: dataRetention ?? this.dataRetention,
      backupFrequency: backupFrequency ?? this.backupFrequency,
      shareAnalytics: shareAnalytics ?? this.shareAnalytics,
      shareCrashReports: shareCrashReports ?? this.shareCrashReports,
      enableExportEncryption: enableExportEncryption ?? this.enableExportEncryption,
    );
  }

  /// Check if enhanced security features are enabled
  bool get hasEnhancedSecurity => enableLocalEncryption;

  /// Get security level description
  String get securityLevel {
    if (enableLocalEncryption && requireAuthForAccess && enableBiometric) {
      return 'Maximum Security';
    } else if (requireAuthForAccess || enableLocalEncryption) {
      return 'High Security';
    } else if (enableBiometric) {
      return 'Medium Security';
    }
    return 'Basic Security';
  }

  @override
  List<Object?> get props => [
        enableLocalEncryption,
        requireAuthForAccess,
        enableBiometric,
        dataRetention,
        backupFrequency,
        shareAnalytics,
        shareCrashReports,
        enableExportEncryption,
      ];

  @override
  String toString() {
    return 'PrivacySettings(encryption: $enableLocalEncryption, auth: $requireAuthForAccess, biometric: $enableBiometric, retention: $dataRetention)';
  }
}
