/**
 * TimeSafari Android Configuration
 * 
 * Provides TimeSafari-specific Android platform configuration including
 * notification channels, permissions, and battery optimization settings.
 * 
 * @author Matthew Raymer
 * @version 1.0.0
 */

/**
 * TimeSafari Android Configuration Interface
 */
export interface TimeSafariAndroidConfig {
  /**
   * Notification channel configuration
   */
  notificationChannels: NotificationChannelConfig[];
  
  /**
   * Permission requirements
   */
  permissions: AndroidPermission[];
  
  /**
   * Battery optimization settings
   */
  batteryOptimization: BatteryOptimizationConfig;
  
  /**
   * Doze and App Standby settings
   */
  powerManagement: PowerManagementConfig;
  
  /**
   * WorkManager constraints
   */
  workManagerConstraints: WorkManagerConstraints;
}

/**
 * Notification Channel Configuration
 */
export interface NotificationChannelConfig {
  id: string;
  name: string;
  description: string;
  importance: 'low' | 'default' | 'high' | 'max';
  enableLights: boolean;
  enableVibration: boolean;
  lightColor: string;
  sound: string | null;
  showBadge: boolean;
  bypassDnd: boolean;
  lockscreenVisibility: 'public' | 'private' | 'secret';
}

/**
 * Android Permission Configuration
 */
export interface AndroidPermission {
  name: string;
  description: string;
  required: boolean;
  runtime: boolean;
  category: 'notification' | 'alarm' | 'network' | 'storage' | 'system';
}

/**
 * Battery Optimization Configuration
 */
export interface BatteryOptimizationConfig {
  exemptPackages: string[];
  whitelistRequestMessage: string;
  optimizationCheckInterval: number; // minutes
  fallbackBehavior: 'graceful' | 'aggressive' | 'disabled';
}

/**
 * Power Management Configuration
 */
export interface PowerManagementConfig {
  dozeModeHandling: 'ignore' | 'adapt' | 'request_whitelist';
  appStandbyHandling: 'ignore' | 'adapt' | 'request_whitelist';
  backgroundRestrictions: 'ignore' | 'adapt' | 'request_whitelist';
  adaptiveBatteryHandling: 'ignore' | 'adapt' | 'request_whitelist';
}

/**
 * WorkManager Constraints Configuration
 */
export interface WorkManagerConstraints {
  networkType: 'not_required' | 'connected' | 'unmetered' | 'not_roaming' | 'metered';
  requiresBatteryNotLow: boolean;
  requiresCharging: boolean;
  requiresDeviceIdle: boolean;
  requiresStorageNotLow: boolean;
  backoffPolicy: 'linear' | 'exponential';
  backoffDelay: number; // milliseconds
  maxRetries: number;
}

/**
 * Default TimeSafari Android Configuration
 */
export const DEFAULT_TIMESAFARI_ANDROID_CONFIG: TimeSafariAndroidConfig = {
  notificationChannels: [
    {
      id: 'timesafari_community_updates',
      name: 'TimeSafari Community Updates',
      description: 'Daily updates from your TimeSafari community including new offers, project updates, and trust network activities',
      importance: 'default',
      enableLights: true,
      enableVibration: true,
      lightColor: '#2196F3',
      sound: 'default',
      showBadge: true,
      bypassDnd: false,
      lockscreenVisibility: 'public'
    },
    {
      id: 'timesafari_project_notifications',
      name: 'TimeSafari Project Notifications',
      description: 'Notifications about starred projects, funding updates, and project milestones',
      importance: 'high',
      enableLights: true,
      enableVibration: true,
      lightColor: '#4CAF50',
      sound: 'default',
      showBadge: true,
      bypassDnd: false,
      lockscreenVisibility: 'public'
    },
    {
      id: 'timesafari_trust_network',
      name: 'TimeSafari Trust Network',
      description: 'Trust network activities, endorsements, and community recommendations',
      importance: 'default',
      enableLights: true,
      enableVibration: false,
      lightColor: '#FF9800',
      sound: null,
      showBadge: true,
      bypassDnd: false,
      lockscreenVisibility: 'public'
    },
    {
      id: 'timesafari_system',
      name: 'TimeSafari System',
      description: 'System notifications, authentication updates, and plugin status messages',
      importance: 'low',
      enableLights: false,
      enableVibration: false,
      lightColor: '#9E9E9E',
      sound: null,
      showBadge: false,
      bypassDnd: false,
      lockscreenVisibility: 'private'
    },
    {
      id: 'timesafari_reminders',
      name: 'TimeSafari Reminders',
      description: 'Personal reminders and daily check-ins for your TimeSafari activities',
      importance: 'default',
      enableLights: true,
      enableVibration: true,
      lightColor: '#9C27B0',
      sound: 'default',
      showBadge: true,
      bypassDnd: false,
      lockscreenVisibility: 'public'
    }
  ],
  
  permissions: [
    {
      name: 'android.permission.POST_NOTIFICATIONS',
      description: 'Allow TimeSafari to show notifications',
      required: true,
      runtime: true,
      category: 'notification'
    },
    {
      name: 'android.permission.SCHEDULE_EXACT_ALARM',
      description: 'Allow TimeSafari to schedule exact alarms for notifications',
      required: true,
      runtime: true,
      category: 'alarm'
    },
    {
      name: 'android.permission.WAKE_LOCK',
      description: 'Allow TimeSafari to keep device awake for background tasks',
      required: true,
      runtime: false,
      category: 'system'
    },
    {
      name: 'android.permission.RECEIVE_BOOT_COMPLETED',
      description: 'Allow TimeSafari to restart notifications after device reboot',
      required: true,
      runtime: false,
      category: 'system'
    },
    {
      name: 'android.permission.INTERNET',
      description: 'Allow TimeSafari to fetch community data and send callbacks',
      required: true,
      runtime: false,
      category: 'network'
    },
    {
      name: 'android.permission.ACCESS_NETWORK_STATE',
      description: 'Allow TimeSafari to check network connectivity',
      required: true,
      runtime: false,
      category: 'network'
    }
  ],
  
  batteryOptimization: {
    exemptPackages: ['org.timesafari.dailynotification'],
    whitelistRequestMessage: 'TimeSafari needs to run in the background to deliver your daily community updates and notifications. Please whitelist TimeSafari from battery optimization.',
    optimizationCheckInterval: 60, // 1 hour
    fallbackBehavior: 'graceful'
  },
  
  powerManagement: {
    dozeModeHandling: 'request_whitelist',
    appStandbyHandling: 'request_whitelist',
    backgroundRestrictions: 'request_whitelist',
    adaptiveBatteryHandling: 'request_whitelist'
  },
  
  workManagerConstraints: {
    networkType: 'connected',
    requiresBatteryNotLow: false,
    requiresCharging: false,
    requiresDeviceIdle: false,
    requiresStorageNotLow: true,
    backoffPolicy: 'exponential',
    backoffDelay: 30000, // 30 seconds
    maxRetries: 3
  }
};

/**
 * TimeSafari Android Configuration Manager
 */
export class TimeSafariAndroidConfigManager {
  private config: TimeSafariAndroidConfig;
  
  constructor(config?: Partial<TimeSafariAndroidConfig>) {
    this.config = { ...DEFAULT_TIMESAFARI_ANDROID_CONFIG, ...config };
  }
  
  /**
   * Get notification channel configuration
   */
  getNotificationChannel(channelId: string): NotificationChannelConfig | undefined {
    return this.config.notificationChannels.find(channel => channel.id === channelId);
  }
  
  /**
   * Get all notification channels
   */
  getAllNotificationChannels(): NotificationChannelConfig[] {
    return this.config.notificationChannels;
  }
  
  /**
   * Get required permissions
   */
  getRequiredPermissions(): AndroidPermission[] {
    return this.config.permissions.filter(permission => permission.required);
  }
  
  /**
   * Get runtime permissions
   */
  getRuntimePermissions(): AndroidPermission[] {
    return this.config.permissions.filter(permission => permission.runtime);
  }
  
  /**
   * Get permissions by category
   */
  getPermissionsByCategory(category: string): AndroidPermission[] {
    return this.config.permissions.filter(permission => permission.category === category);
  }
  
  /**
   * Get battery optimization configuration
   */
  getBatteryOptimizationConfig(): BatteryOptimizationConfig {
    return this.config.batteryOptimization;
  }
  
  /**
   * Get power management configuration
   */
  getPowerManagementConfig(): PowerManagementConfig {
    return this.config.powerManagement;
  }
  
  /**
   * Get WorkManager constraints
   */
  getWorkManagerConstraints(): WorkManagerConstraints {
    return this.config.workManagerConstraints;
  }
  
  /**
   * Update configuration
   */
  updateConfig(updates: Partial<TimeSafariAndroidConfig>): void {
    this.config = { ...this.config, ...updates };
  }
  
  /**
   * Validate configuration
   */
  validateConfig(): { valid: boolean; errors: string[] } {
    const errors: string[] = [];
    
    // Validate notification channels
    if (this.config.notificationChannels.length === 0) {
      errors.push('At least one notification channel must be configured');
    }
    
    // Validate permissions
    const requiredPermissions = this.getRequiredPermissions();
    if (requiredPermissions.length === 0) {
      errors.push('At least one required permission must be configured');
    }
    
    // Validate WorkManager constraints
    if (this.config.workManagerConstraints.maxRetries < 0) {
      errors.push('WorkManager maxRetries must be non-negative');
    }
    
    if (this.config.workManagerConstraints.backoffDelay < 0) {
      errors.push('WorkManager backoffDelay must be non-negative');
    }
    
    return {
      valid: errors.length === 0,
      errors
    };
  }
}
