/**
 * Client-side self-telemetry for the admin dashboard.
 *
 * Delegates to @bytelyst/telemetry-client shared package.
 * Sends to platform-service via /api/telemetry/admin-ingest proxy.
 *
 * Privacy: No PII. Only page paths, action names, and timing metrics.
 * See docs/WINDSURF/CLIENT_TELEMETRY_DESIGN.md
 */

import { createTelemetryClient, type TelemetryClient } from '@bytelyst/telemetry-client';

const PRODUCT_ID = process.env.NEXT_PUBLIC_PRODUCT_ID || 'unknown';

let _client: TelemetryClient | null = null;

function getClient(): TelemetryClient {
  if (!_client) {
    _client = createTelemetryClient({
      productId: PRODUCT_ID,
      baseUrl: '',
      endpoint: '/api/telemetry/admin-ingest',
      platform: 'web',
      channel: 'web_app',
      transport: 'beacon',
      appVersion: '0.0.0',
      buildNumber: '0',
      releaseChannel: 'beta',
    });
  }
  return _client;
}

export function trackEvent(
  eventType: 'debug' | 'info' | 'warn' | 'error',
  module: string,
  eventName: string,
  options?: {
    feature?: string;
    message?: string;
    tags?: Record<string, string>;
    metrics?: Record<string, number>;
  }
): void {
  if (typeof window === 'undefined') return;
  getClient().trackEvent(eventType, module, eventName, options);
}

export function trackPageView(path: string): void {
  trackEvent('info', 'navigation', 'page_view', {
    tags: { path },
  });
}

export function flush(): void {
  if (typeof window === 'undefined') return;
  getClient().flush();
}

export function initTelemetry(): void {
  if (typeof window === 'undefined') return;
  getClient().init();
}
