/**
 * GlitchTip Error Tracking Adapter
 * GlitchTip is Sentry-compatible.
 */

import type { ErrorTrackingAdapter, ErrorContext, ErrorTrackingConfig } from './interfaces';

export class GlitchTipAdapter implements ErrorTrackingAdapter {
  private dsn: string;

  constructor(config: ErrorTrackingConfig) {
    this.dsn = config.dsn;
    this.init(config);
  }

  private init(config: ErrorTrackingConfig) {
    if (typeof window === 'undefined') return;

    // In a real scenario, we would import @sentry/nextjs or @sentry/browser
    // For this implementation, we assume Sentry is available globally or 
    // we provide the structure that would call the SDK.
    const w = window as any;
    if (w.Sentry) {
      w.Sentry.init({
        dsn: this.dsn,
        environment: config.environment || 'production',
        release: config.release,
        debug: config.debug || false,
      });
    }
  }

  captureException(error: any, context?: ErrorContext): void {
    if (typeof window === 'undefined') return;
    const w = window as any;
    if (w.Sentry) {
      w.Sentry.captureException(error, context);
    } else {
      console.error('[GlitchTip] Exception captured (Sentry not loaded):', error, context);
    }
  }

  captureMessage(message: string, context?: ErrorContext): void {
    if (typeof window === 'undefined') return;
    const w = window as any;
    if (w.Sentry) {
      w.Sentry.captureMessage(message, context);
    } else {
      console.log('[GlitchTip] Message captured (Sentry not loaded):', message, context);
    }
  }

  setUser(user: ErrorContext['user']): void {
    if (typeof window === 'undefined') return;
    const w = window as any;
    if (w.Sentry) {
      w.Sentry.setUser(user);
    }
  }

  setTag(key: string, value: string): void {
    if (typeof window === 'undefined') return;
    const w = window as any;
    if (w.Sentry) {
      w.Sentry.setTag(key, value);
    }
  }

  setExtra(key: string, value: any): void {
    if (typeof window === 'undefined') return;
    const w = window as any;
    if (w.Sentry) {
      w.Sentry.setExtra(key, value);
    }
  }
}
