/**
 * Platform Configuration Tests
 * 
 * Tests for TimeSafari-specific platform configurations including
 * Android and iOS platform settings.
 * 
 * @author Matthew Raymer
 * @version 1.0.0
 */

import {
  TimeSafariAndroidConfigManager,
  TimeSafariAndroidConfig
} from '../android/timesafari-android-config';

import {
  TimeSafariIOSConfigManager,
  TimeSafariIOSConfig
} from '../ios/timesafari-ios-config';

describe('Platform Configuration', () => {
  describe('Android Configuration', () => {
    let androidConfig: TimeSafariAndroidConfigManager;

    beforeEach(() => {
      androidConfig = new TimeSafariAndroidConfigManager();
    });

    test('should initialize with default configuration', () => {
      expect(androidConfig).toBeDefined();
      
      const notificationChannels = androidConfig.getAllNotificationChannels();
      expect(notificationChannels).toHaveLength(5);
      expect(notificationChannels[0].id).toBe('timesafari_community_updates');
      expect(notificationChannels[0].name).toBe('TimeSafari Community Updates');
      expect(notificationChannels[0].importance).toBe('default');
    });

    test('should get notification channels configuration', () => {
      const channels = androidConfig.getAllNotificationChannels();
      expect(channels).toHaveLength(5);
      
      const communityChannel = channels.find(c => c.id === 'timesafari_community_updates');
      expect(communityChannel).toBeDefined();
      expect(communityChannel?.name).toBe('TimeSafari Community Updates');
      expect(communityChannel?.importance).toBe('default');
      expect(communityChannel?.sound).toBe('default');
      expect(communityChannel?.enableVibration).toBe(true);
    });

    test('should get required permissions', () => {
      const permissions = androidConfig.getRequiredPermissions();
      expect(permissions.some(p => p.name === 'android.permission.POST_NOTIFICATIONS')).toBe(true);
      expect(permissions.some(p => p.name === 'android.permission.SCHEDULE_EXACT_ALARM')).toBe(true);
      expect(permissions.some(p => p.name === 'android.permission.WAKE_LOCK')).toBe(true);
      expect(permissions.some(p => p.name === 'android.permission.RECEIVE_BOOT_COMPLETED')).toBe(true);
    });

    test('should get battery optimization settings', () => {
      const batterySettings = androidConfig.getBatteryOptimizationConfig();
      expect(batterySettings.exemptPackages).toContain('org.timesafari.dailynotification');
      expect(batterySettings.whitelistRequestMessage).toBeDefined();
      expect(batterySettings.optimizationCheckInterval).toBeGreaterThan(0);
    });

    test('should get WorkManager constraints', () => {
      const constraints = androidConfig.getWorkManagerConstraints();
      expect(constraints.networkType).toBe('connected');
      expect(constraints.requiresBatteryNotLow).toBe(false);
      expect(constraints.requiresCharging).toBe(false);
      expect(constraints.requiresDeviceIdle).toBe(false);
      expect(constraints.requiresStorageNotLow).toBe(true);
    });

    test('should update configuration', () => {
      const updates: Partial<TimeSafariAndroidConfig> = {
        notificationChannels: [
          {
            id: 'custom_channel',
            name: 'Custom Channel',
            description: 'Custom notification channel',
            importance: 'default',
            enableLights: false,
            enableVibration: false,
            lightColor: '#000000',
            sound: null,
            showBadge: true,
            bypassDnd: false,
            lockscreenVisibility: 'public'
          }
        ]
      };

      androidConfig.updateConfig(updates);
      
      const channels = androidConfig.getAllNotificationChannels();
      expect(channels).toHaveLength(1);
      expect(channels[0].id).toBe('custom_channel');
    });

    test('should validate configuration', () => {
      const validation = androidConfig.validateConfig();
      expect(validation.valid).toBe(true);
      expect(validation.errors).toEqual([]);
    });

    test('should handle configuration updates', () => {
      const updates: Partial<TimeSafariAndroidConfig> = {
        notificationChannels: [
          {
            id: 'test_channel',
            name: 'Test Channel',
            description: 'Test channel',
            importance: 'default',
            enableLights: false,
            enableVibration: true,
            lightColor: '#000000',
            sound: 'default',
            showBadge: true,
            bypassDnd: false,
            lockscreenVisibility: 'public'
          }
        ]
      };

      androidConfig.updateConfig(updates);
      const validation = androidConfig.validateConfig();

      expect(validation.valid).toBe(true);
    });
  });

  describe('iOS Configuration', () => {
    let iosConfig: TimeSafariIOSConfigManager;

    beforeEach(() => {
      iosConfig = new TimeSafariIOSConfigManager();
    });

    test('should initialize with default configuration', () => {
      expect(iosConfig).toBeDefined();
      
      const notificationCategories = iosConfig.getAllNotificationCategories();
      expect(notificationCategories).toHaveLength(4);
      expect(notificationCategories[0].identifier).toBe('TIMESAFARI_COMMUNITY_UPDATE');
      expect(notificationCategories[0].actions).toHaveLength(3);
    });

    test('should get notification categories configuration', () => {
      const categories = iosConfig.getAllNotificationCategories();
      expect(categories).toHaveLength(4);
      
      const communityCategory = categories.find(c => c.identifier === 'TIMESAFARI_COMMUNITY_UPDATE');
      expect(communityCategory).toBeDefined();
      expect(communityCategory?.actions).toHaveLength(3);
      expect(communityCategory?.intentIdentifiers).toEqual([]);
    });

    test('should get background tasks', () => {
      const backgroundTasks = iosConfig.getAllBackgroundTasks();
      expect(backgroundTasks).toHaveLength(2);
      expect(backgroundTasks[0].identifier).toBe('org.timesafari.dailynotification.fetch');
      expect(backgroundTasks[1].identifier).toBe('org.timesafari.dailynotification.notify');
    });

    test('should update configuration', () => {
      const updates: Partial<TimeSafariIOSConfig> = {
        notificationCategories: [
          {
            identifier: 'custom_category',
            actions: [
              {
                identifier: 'CUSTOM_ACTION',
                title: 'Custom Action',
                options: {
                  foreground: true,
                  destructive: false,
                  authenticationRequired: false
                }
              }
            ],
            intentIdentifiers: ['CUSTOM_INTENT'],
            hiddenPreviewsBodyPlaceholder: 'Custom placeholder',
            categorySummaryFormat: '%u more',
            options: {
              customDismissAction: false,
              allowInCarPlay: true,
              allowAnnouncement: false,
              showTitle: true,
              showSubtitle: true,
              showBody: true
            }
          }
        ]
      };

      iosConfig.updateConfig(updates);
      
      const categories = iosConfig.getAllNotificationCategories();
      expect(categories).toHaveLength(1);
      expect(categories[0].identifier).toBe('custom_category');
    });

    test('should validate configuration', () => {
      const validation = iosConfig.validateConfig();
      expect(validation.valid).toBe(true);
      expect(validation.errors).toEqual([]);
    });

    test('should handle configuration updates', () => {
      const updates: Partial<TimeSafariIOSConfig> = {
        notificationCategories: [
          {
            identifier: 'test_category',
            actions: [
              {
                identifier: 'TEST_ACTION',
                title: 'Test Action',
                options: {
                  foreground: false,
                  destructive: false,
                  authenticationRequired: false
                }
              }
            ],
            intentIdentifiers: [],
            hiddenPreviewsBodyPlaceholder: 'Test',
            categorySummaryFormat: '%u more',
            options: {
              customDismissAction: false,
              allowInCarPlay: true,
              allowAnnouncement: false,
              showTitle: true,
              showSubtitle: true,
              showBody: true
            }
          }
        ]
      };

      iosConfig.updateConfig(updates);
      const validation = iosConfig.validateConfig();

      expect(validation.valid).toBe(true);
    });
  });

  describe('Cross-Platform Consistency', () => {
    test('should have consistent notification channel/category structure', () => {
      const androidConfig = new TimeSafariAndroidConfigManager();
      const iosConfig = new TimeSafariIOSConfigManager();

      const androidChannels = androidConfig.getAllNotificationChannels();
      const iosCategories = iosConfig.getAllNotificationCategories();

      // Both should have notification types (may have different counts)
      expect(androidChannels.length).toBeGreaterThan(0);
      expect(iosCategories.length).toBeGreaterThan(0);

      // Both should have community updates as the first item
      expect(androidChannels[0].id).toBe('timesafari_community_updates');
      expect(iosCategories[0].identifier).toBe('TIMESAFARI_COMMUNITY_UPDATE');
    });

    test('should have consistent permission requirements', () => {
      const androidConfig = new TimeSafariAndroidConfigManager();
      const iosConfig = new TimeSafariIOSConfigManager();

      const androidPermissions = androidConfig.getRequiredPermissions();
      const iosCategories = iosConfig.getAllNotificationCategories();

      // Both should require notification permissions
      expect(androidPermissions.some(p => p.name === 'android.permission.POST_NOTIFICATIONS')).toBe(true);
      expect(iosCategories.length).toBeGreaterThan(0);
    });

    test('should have consistent background task support', () => {
      const androidConfig = new TimeSafariAndroidConfigManager();
      const iosConfig = new TimeSafariIOSConfigManager();

      const androidConstraints = androidConfig.getWorkManagerConstraints();
      const iosBackgroundTasks = iosConfig.getAllBackgroundTasks();

      // Both should support background processing
      expect(androidConstraints.networkType).toBe('connected');
      expect(iosBackgroundTasks.length).toBeGreaterThan(0);
    });
  });
});