/**
 * TimeSafari iOS Configuration
 * 
 * Provides TimeSafari-specific iOS platform configuration including
 * notification categories, background tasks, and permission handling.
 * 
 * @author Matthew Raymer
 * @version 1.0.0
 */

/**
 * TimeSafari iOS Configuration Interface
 */
export interface TimeSafariIOSConfig {
  /**
   * Notification categories configuration
   */
  notificationCategories: NotificationCategoryConfig[];
  
  /**
   * Background task configuration
   */
  backgroundTasks: BackgroundTaskConfig[];
  
  /**
   * Permission configuration
   */
  permissions: IOSPermission[];
  
  /**
   * Background App Refresh settings
   */
  backgroundAppRefresh: BackgroundAppRefreshConfig;
  
  /**
   * Notification center settings
   */
  notificationCenter: NotificationCenterConfig;
}

/**
 * Notification Category Configuration
 */
export interface NotificationCategoryConfig {
  identifier: string;
  actions: NotificationActionConfig[];
  intentIdentifiers: string[];
  hiddenPreviewsBodyPlaceholder: string;
  categorySummaryFormat: string;
  options: NotificationCategoryOptions;
}

/**
 * Notification Action Configuration
 */
export interface NotificationActionConfig {
  identifier: string;
  title: string;
  options: NotificationActionOptions;
  textInputButtonTitle?: string;
  textInputPlaceholder?: string;
}

/**
 * Background Task Configuration
 */
export interface BackgroundTaskConfig {
  identifier: string;
  name: string;
  description: string;
  estimatedDuration: number; // seconds
  requiresNetworkConnectivity: boolean;
  requiresExternalPower: boolean;
  priority: 'low' | 'default' | 'high';
}

/**
 * iOS Permission Configuration
 */
export interface IOSPermission {
  type: 'notifications' | 'backgroundAppRefresh' | 'backgroundProcessing';
  description: string;
  required: boolean;
  provisional: boolean;
  options: NotificationPermissionOptions;
}

/**
 * Background App Refresh Configuration
 */
export interface BackgroundAppRefreshConfig {
  enabled: boolean;
  allowedInLowPowerMode: boolean;
  allowedInBackground: boolean;
  minimumInterval: number; // seconds
  maximumDuration: number; // seconds
}

/**
 * Notification Center Configuration
 */
export interface NotificationCenterConfig {
  delegateClassName: string;
  categories: string[];
  settings: NotificationCenterSettings;
}

/**
 * Notification Category Options
 */
export interface NotificationCategoryOptions {
  customDismissAction: boolean;
  allowInCarPlay: boolean;
  allowAnnouncement: boolean;
  showTitle: boolean;
  showSubtitle: boolean;
  showBody: boolean;
}

/**
 * Notification Action Options
 */
export interface NotificationActionOptions {
  foreground: boolean;
  destructive: boolean;
  authenticationRequired: boolean;
}

/**
 * Notification Permission Options
 */
export interface NotificationPermissionOptions {
  alert: boolean;
  badge: boolean;
  sound: boolean;
  carPlay: boolean;
  criticalAlert: boolean;
  provisional: boolean;
  providesAppNotificationSettings: boolean;
  announcement: boolean;
}

/**
 * Notification Center Settings
 */
export interface NotificationCenterSettings {
  authorizationStatus: 'notDetermined' | 'denied' | 'authorized' | 'provisional';
  alertSetting: 'notSupported' | 'disabled' | 'enabled';
  badgeSetting: 'notSupported' | 'disabled' | 'enabled';
  soundSetting: 'notSupported' | 'disabled' | 'enabled';
  carPlaySetting: 'notSupported' | 'disabled' | 'enabled';
  criticalAlertSetting: 'notSupported' | 'disabled' | 'enabled';
  providesAppNotificationSettings: boolean;
  announcementSetting: 'notSupported' | 'disabled' | 'enabled';
}

/**
 * Default TimeSafari iOS Configuration
 */
export const DEFAULT_TIMESAFARI_IOS_CONFIG: TimeSafariIOSConfig = {
  notificationCategories: [
    {
      identifier: 'TIMESAFARI_COMMUNITY_UPDATE',
      actions: [
        {
          identifier: 'VIEW_OFFERS',
          title: 'View Offers',
          options: {
            foreground: true,
            destructive: false,
            authenticationRequired: false
          }
        },
        {
          identifier: 'VIEW_PROJECTS',
          title: 'View Projects',
          options: {
            foreground: true,
            destructive: false,
            authenticationRequired: false
          }
        },
        {
          identifier: 'DISMISS',
          title: 'Dismiss',
          options: {
            foreground: false,
            destructive: false,
            authenticationRequired: false
          }
        }
      ],
      intentIdentifiers: [],
      hiddenPreviewsBodyPlaceholder: 'New TimeSafari community update available',
      categorySummaryFormat: '%u more community updates',
      options: {
        customDismissAction: true,
        allowInCarPlay: false,
        allowAnnouncement: true,
        showTitle: true,
        showSubtitle: true,
        showBody: true
      }
    },
    {
      identifier: 'TIMESAFARI_PROJECT_UPDATE',
      actions: [
        {
          identifier: 'VIEW_PROJECT',
          title: 'View Project',
          options: {
            foreground: true,
            destructive: false,
            authenticationRequired: false
          }
        },
        {
          identifier: 'STAR_PROJECT',
          title: 'Star Project',
          options: {
            foreground: false,
            destructive: false,
            authenticationRequired: false
          }
        },
        {
          identifier: 'DISMISS',
          title: 'Dismiss',
          options: {
            foreground: false,
            destructive: false,
            authenticationRequired: false
          }
        }
      ],
      intentIdentifiers: [],
      hiddenPreviewsBodyPlaceholder: 'New project update available',
      categorySummaryFormat: '%u more project updates',
      options: {
        customDismissAction: true,
        allowInCarPlay: false,
        allowAnnouncement: true,
        showTitle: true,
        showSubtitle: true,
        showBody: true
      }
    },
    {
      identifier: 'TIMESAFARI_TRUST_NETWORK',
      actions: [
        {
          identifier: 'VIEW_ENDORSEMENT',
          title: 'View Endorsement',
          options: {
            foreground: true,
            destructive: false,
            authenticationRequired: false
          }
        },
        {
          identifier: 'DISMISS',
          title: 'Dismiss',
          options: {
            foreground: false,
            destructive: false,
            authenticationRequired: false
          }
        }
      ],
      intentIdentifiers: [],
      hiddenPreviewsBodyPlaceholder: 'New trust network activity',
      categorySummaryFormat: '%u more trust network updates',
      options: {
        customDismissAction: true,
        allowInCarPlay: false,
        allowAnnouncement: false,
        showTitle: true,
        showSubtitle: true,
        showBody: true
      }
    },
    {
      identifier: 'TIMESAFARI_REMINDER',
      actions: [
        {
          identifier: 'COMPLETE_TASK',
          title: 'Complete Task',
          options: {
            foreground: true,
            destructive: false,
            authenticationRequired: false
          }
        },
        {
          identifier: 'SNOOZE',
          title: 'Snooze 1 Hour',
          options: {
            foreground: false,
            destructive: false,
            authenticationRequired: false
          }
        },
        {
          identifier: 'DISMISS',
          title: 'Dismiss',
          options: {
            foreground: false,
            destructive: false,
            authenticationRequired: false
          }
        }
      ],
      intentIdentifiers: [],
      hiddenPreviewsBodyPlaceholder: 'TimeSafari reminder',
      categorySummaryFormat: '%u more reminders',
      options: {
        customDismissAction: true,
        allowInCarPlay: true,
        allowAnnouncement: true,
        showTitle: true,
        showSubtitle: true,
        showBody: true
      }
    }
  ],
  
  backgroundTasks: [
    {
      identifier: 'org.timesafari.dailynotification.fetch',
      name: 'TimeSafari Content Fetch',
      description: 'Fetch daily community content and project updates',
      estimatedDuration: 30,
      requiresNetworkConnectivity: true,
      requiresExternalPower: false,
      priority: 'default'
    },
    {
      identifier: 'org.timesafari.dailynotification.notify',
      name: 'TimeSafari Notification Delivery',
      description: 'Deliver scheduled notifications to user',
      estimatedDuration: 10,
      requiresNetworkConnectivity: false,
      requiresExternalPower: false,
      priority: 'high'
    }
  ],
  
  permissions: [
    {
      type: 'notifications',
      description: 'Allow TimeSafari to send notifications about community updates and project activities',
      required: true,
      provisional: true,
      options: {
        alert: true,
        badge: true,
        sound: true,
        carPlay: false,
        criticalAlert: false,
        provisional: true,
        providesAppNotificationSettings: true,
        announcement: true
      }
    },
    {
      type: 'backgroundAppRefresh',
      description: 'Allow TimeSafari to refresh content in the background',
      required: true,
      provisional: false,
      options: {
        alert: false,
        badge: false,
        sound: false,
        carPlay: false,
        criticalAlert: false,
        provisional: false,
        providesAppNotificationSettings: false,
        announcement: false
      }
    },
    {
      type: 'backgroundProcessing',
      description: 'Allow TimeSafari to process background tasks',
      required: true,
      provisional: false,
      options: {
        alert: false,
        badge: false,
        sound: false,
        carPlay: false,
        criticalAlert: false,
        provisional: false,
        providesAppNotificationSettings: false,
        announcement: false
      }
    }
  ],
  
  backgroundAppRefresh: {
    enabled: true,
    allowedInLowPowerMode: false,
    allowedInBackground: true,
    minimumInterval: 300, // 5 minutes
    maximumDuration: 30 // 30 seconds
  },
  
  notificationCenter: {
    delegateClassName: 'TimeSafariNotificationCenterDelegate',
    categories: [
      'TIMESAFARI_COMMUNITY_UPDATE',
      'TIMESAFARI_PROJECT_UPDATE',
      'TIMESAFARI_TRUST_NETWORK',
      'TIMESAFARI_REMINDER'
    ],
    settings: {
      authorizationStatus: 'notDetermined',
      alertSetting: 'enabled',
      badgeSetting: 'enabled',
      soundSetting: 'enabled',
      carPlaySetting: 'disabled',
      criticalAlertSetting: 'disabled',
      providesAppNotificationSettings: true,
      announcementSetting: 'enabled'
    }
  }
};

/**
 * TimeSafari iOS Configuration Manager
 */
export class TimeSafariIOSConfigManager {
  private config: TimeSafariIOSConfig;
  
  constructor(config?: Partial<TimeSafariIOSConfig>) {
    this.config = { ...DEFAULT_TIMESAFARI_IOS_CONFIG, ...config };
  }
  
  /**
   * Get notification category configuration
   */
  getNotificationCategory(categoryId: string): NotificationCategoryConfig | undefined {
    return this.config.notificationCategories.find(category => category.identifier === categoryId);
  }
  
  /**
   * Get all notification categories
   */
  getAllNotificationCategories(): NotificationCategoryConfig[] {
    return this.config.notificationCategories;
  }
  
  /**
   * Get background task configuration
   */
  getBackgroundTask(taskId: string): BackgroundTaskConfig | undefined {
    return this.config.backgroundTasks.find(task => task.identifier === taskId);
  }
  
  /**
   * Get all background tasks
   */
  getAllBackgroundTasks(): BackgroundTaskConfig[] {
    return this.config.backgroundTasks;
  }
  
  /**
   * Get required permissions
   */
  getRequiredPermissions(): IOSPermission[] {
    return this.config.permissions.filter(permission => permission.required);
  }
  
  /**
   * Get provisional permissions
   */
  getProvisionalPermissions(): IOSPermission[] {
    return this.config.permissions.filter(permission => permission.provisional);
  }
  
  /**
   * Get background app refresh configuration
   */
  getBackgroundAppRefreshConfig(): BackgroundAppRefreshConfig {
    return this.config.backgroundAppRefresh;
  }
  
  /**
   * Get notification center configuration
   */
  getNotificationCenterConfig(): NotificationCenterConfig {
    return this.config.notificationCenter;
  }
  
  /**
   * Update configuration
   */
  updateConfig(updates: Partial<TimeSafariIOSConfig>): void {
    this.config = { ...this.config, ...updates };
  }
  
  /**
   * Validate configuration
   */
  validateConfig(): { valid: boolean; errors: string[] } {
    const errors: string[] = [];
    
    // Validate notification categories
    if (this.config.notificationCategories.length === 0) {
      errors.push('At least one notification category must be configured');
    }
    
    // Validate background tasks
    if (this.config.backgroundTasks.length === 0) {
      errors.push('At least one background task must be configured');
    }
    
    // Validate permissions
    const requiredPermissions = this.getRequiredPermissions();
    if (requiredPermissions.length === 0) {
      errors.push('At least one required permission must be configured');
    }
    
    // Validate background app refresh
    if (this.config.backgroundAppRefresh.minimumInterval < 0) {
      errors.push('Background app refresh minimum interval must be non-negative');
    }
    
    if (this.config.backgroundAppRefresh.maximumDuration < 0) {
      errors.push('Background app refresh maximum duration must be non-negative');
    }
    
    return {
      valid: errors.length === 0,
      errors
    };
  }
}
