import * as amplitude from '@amplitude/analytics-browser';
import { BaseAnalytics } from './Analytics';

export class AmplitudeAnalytics extends BaseAnalytics {
  public name = 'AmplitudeAnalytics';
  private initialized = false;

  constructor(apiKey: string) {
    super();
    amplitude.init(apiKey).promise.then(() => {
      this.initialized = true;
      console.log('[Amplitude] initialized');
    }).catch((err) => {
      console.error('[Amplitude] init failed', err);
    });
  }

  track(name: string, data?: Record<string, any>): void {
    if (!this.initialized) {
      console.warn('[Amplitude] not initialized, event skipped:', name);
      return;
    }
    amplitude.track(name, {
      ...data,
      ts: this.getTimestamp(),
    });
  }

  flush(): void {
    // Amplitude handles batching internally; no manual flush needed
  }
}
