/**
 * TimeSafari Integration Example
 * 
 * Demonstrates how to integrate the Daily Notification Plugin with TimeSafari's
 * privacy-preserving claims architecture, community features, and storage patterns.
 * 
 * @author Matthew Raymer
 * @version 1.0.0
 */

import { 
  DailyNotification,
  TimeSafariIntegrationService,
  TimeSafariStorageAdapterImpl,
  TimeSafariCommunityIntegrationService,
  StorageFactory,
  TimeSafariIntegrationConfig,
  CommunityIntegrationConfig
} from '@timesafari/daily-notification-plugin';

/**
 * TimeSafari Integration Example
 * 
 * This example shows how to integrate the plugin with TimeSafari's architecture:
 * - Privacy-preserving claims with DIDs
 * - Community features with rate limiting
 * - Storage adapter pattern
 * - Observability and monitoring
 */
export class TimeSafariIntegrationExample {
  private integrationService: TimeSafariIntegrationService;
  private communityService: TimeSafariCommunityIntegrationService;
  private storageAdapter: TimeSafariStorageAdapterImpl;

  constructor() {
    this.integrationService = TimeSafariIntegrationService.getInstance();
    this.communityService = TimeSafariCommunityIntegrationService.getInstance();
  }

  /**
   * Initialize TimeSafari integration
   */
  async initialize(): Promise<void> {
    try {
      // Initializing TimeSafari integration...

      // Create storage adapter
      const storage = StorageFactory.createStorage('android');
      this.storageAdapter = new TimeSafariStorageAdapterImpl(storage, 'timesafari_notifications');

      // Initialize TimeSafari integration service
      const integrationConfig: TimeSafariIntegrationConfig = {
        activeDid: 'did:example:timesafari-user-123',
        storageAdapter: this.storageAdapter,
        endorserApiBaseUrl: 'https://endorser.ch/api/v1'
      };

      await this.integrationService.initialize(integrationConfig);

      // Initialize community integration service
      const communityConfig: CommunityIntegrationConfig = {
        maxRequestsPerMinute: 30,
        maxRequestsPerHour: 1000,
        burstLimit: 10,
        initialBackoffMs: 1000,
        maxBackoffMs: 30000,
        backoffMultiplier: 2,
        jitterEnabled: true,
        basePollingIntervalMs: 300000, // 5 minutes
        maxPollingIntervalMs: 1800000, // 30 minutes
        adaptivePolling: true
      };

      await this.communityService.initialize(communityConfig);

      // Log: TimeSafari integration initialized successfully
    } catch (error) {
      throw new Error(`Initialization failed: ${error}`);
    }
  }

  /**
   * Schedule TimeSafari daily notifications
   */
  async scheduleTimeSafariNotifications(): Promise<void> {
    try {
      // Log: Scheduling TimeSafari daily notifications

      // Schedule content fetch for community data
      await DailyNotification.scheduleContentFetch({
        schedule: '0 8 * * *', // 8 AM daily
        ttlSeconds: 3600, // 1 hour TTL
        source: 'timesafari_community',
        url: 'https://endorser.ch/api/v1/community-updates',
        headers: {
          'Authorization': 'Bearer did-jwt-token',
          'Content-Type': 'application/json'
        }
      });

      // Schedule user notification
      await DailyNotification.scheduleUserNotification({
        schedule: '0 9 * * *', // 9 AM daily
        title: 'TimeSafari Community Update',
        body: 'New offers and project updates available',
        actions: [
          { id: 'view_offers', title: 'View Offers' },
          { id: 'view_projects', title: 'View Projects' },
          { id: 'dismiss', title: 'Dismiss' }
        ]
      });

      // Register callback for analytics
      await DailyNotification.registerCallback('timesafari_analytics', {
        kind: 'http',
        target: 'https://analytics.timesafari.com/events',
        headers: {
          'Authorization': 'Bearer analytics-token',
          'Content-Type': 'application/json'
        }
      });

      // Log: TimeSafari notifications scheduled successfully
    } catch (error) {
      throw new Error(`Failed to schedule notifications: ${error}`);
    }
  }

  /**
   * Fetch community data with rate limiting
   */
  async fetchCommunityData(): Promise<void> {
    try {
      // Log: Fetching community data

      const userConfig = {
        activeDid: 'did:example:timesafari-user-123',
        fetchOffersToPerson: true,
        fetchOffersToProjects: true,
        fetchProjectUpdates: true,
        starredPlanIds: ['plan-1', 'plan-2', 'plan-3']
      };

      const bundle = await this.communityService.fetchCommunityDataWithRateLimit(userConfig);
      
      // Log: Community data fetched with success: bundle.success

      // Store in TimeSafari storage
      await this.storageAdapter.store('community_data', bundle, 3600); // 1 hour TTL

    } catch (error) {
      throw new Error(`Failed to fetch community data: ${error}`);
    }
  }

  /**
   * Demonstrate DID-signed payloads
   */
  async demonstrateDidPayloads(): Promise<void> {
    try {
      // Log: Demonstrating DID-signed payloads

      const samplePayloads = this.integrationService.generateSampleDidPayloads();
      
      for (const payload of samplePayloads) {
        // Log: Sample payload type: payload.type

        // Verify signature (placeholder)
        await this.integrationService.verifyDidSignature(
          JSON.stringify(payload.payload),
          payload.signature
        );
        
        // Log: Signature verification completed
      }

    } catch (error) {
      throw new Error(`Failed to demonstrate DID payloads: ${error}`);
    }
  }

  /**
   * Demonstrate data retention and redaction
   */
  async demonstrateDataRetention(): Promise<void> {
    try {
      // Log: Demonstrating data retention and redaction

      this.integrationService.getDataRetentionPolicy();
      
      // Log: Data retention policy configured

      // Store sample data with different retention policies
      await this.storageAdapter.store('session_data', { activeDid: 'did:example:123' }, 3600);
      await this.storageAdapter.store('notification_content', { title: 'Test', body: 'Test' }, 604800); // 7 days
      await this.storageAdapter.store('callback_events', { event: 'delivered' }, 2592000); // 30 days
      await this.storageAdapter.store('performance_metrics', { latency: 100 }, 7776000); // 90 days

      // Get storage statistics
      await this.storageAdapter.getStats();
      // Log: Storage statistics available

    } catch (error) {
      throw new Error(`Failed to demonstrate data retention: ${error}`);
    }
  }

  /**
   * Monitor rate limiting and backoff
   */
  async monitorRateLimiting(): Promise<void> {
    try {
      // Log: Monitoring rate limiting and backoff

      // Get current status
      this.communityService.getRateLimitStatus();
      this.communityService.getBackoffStatus();
      this.communityService.getPollingStatus();

      // Log: Rate limit status available
      // Log: Backoff status available  
      // Log: Polling status available

      // Get optimized polling interval
      this.communityService.getOptimizedPollingInterval();
      // Log: Optimized polling interval available

    } catch (error) {
      throw new Error(`Failed to monitor rate limiting: ${error}`);
    }
  }

  /**
   * Run complete TimeSafari integration example
   */
  async runExample(): Promise<void> {
    try {
      // Log: Running complete TimeSafari integration example

      // Initialize
      await this.initialize();

      // Schedule notifications
      await this.scheduleTimeSafariNotifications();

      // Fetch community data
      await this.fetchCommunityData();

      // Demonstrate DID payloads
      await this.demonstrateDidPayloads();

      // Demonstrate data retention
      await this.demonstrateDataRetention();

      // Monitor rate limiting
      await this.monitorRateLimiting();

      // Log: Complete TimeSafari integration example finished successfully
    } catch (error) {
      throw new Error(`Example failed: ${error}`);
    }
  }
}

/**
 * Usage Example
 */
export async function runTimeSafariIntegrationExample(): Promise<void> {
  const example = new TimeSafariIntegrationExample();
  await example.runExample();
}

// Export for use in other modules
export default TimeSafariIntegrationExample;
