import { getSettings } from '../db/repositories/settingsRepository';

export interface AnalyticsEvent {
  name: string;
  properties?: Record<string, any>;
}

/**
 * Analytics abstraction with privacy controls
 * By default, all events are no-op unless user opts in
 */
class Analytics {
  private optedIn: boolean = false;

  async initialize(userId?: string): Promise<void> {
    // Analytics opt-in will be loaded once user is logged in
    // For now, default to false
    this.optedIn = false;
  }

  setOptIn(optIn: boolean): void {
    this.optedIn = optIn;
  }

  track(eventName: string, properties?: Record<string, any>): void {
    if (!this.optedIn) return;

    // In a real app, this would send to analytics service
    // For now, just log in dev mode
    if (__DEV__) {
      console.log('[Analytics]', eventName, properties);
    }

    // TODO: Integrate with Sentry, Amplitude, or similar
    // Example: Sentry.addBreadcrumb({ category: 'analytics', message: eventName, data: properties });
  }

  // Convenience methods for common events
  appOpen(cold: boolean): void {
    this.track('app_open', { cold });
  }

  projectCreated(withCover: boolean): void {
    this.track('project_created', { with_cover: withCover });
  }

  stepAdded(type: string, hasPhotos: boolean): void {
    this.track('step_added', { type, has_photos: hasPhotos });
  }

  firingLogged(cone: string, tempUnit: string): void {
    this.track('firing_logged', { cone, temp_unit: tempUnit });
  }

  glazeAddedToStep(brand: string, isCustom: boolean, coats: number): void {
    this.track('glaze_added_to_step', { brand, is_custom: isCustom, coats });
  }

  settingsChanged(unitSystem: string, tempUnit: string): void {
    this.track('settings_changed', { unitSystem, tempUnit });
  }

  exportDone(format: string): void {
    this.track('export_done', { format });
  }

  contactSubmitted(success: boolean): void {
    this.track('contact_submitted', { success });
  }
}

export const analytics = new Analytics();
