/**
 * Types for @bytelyst/telemetry-client.
 * Browser/React Native-safe — no Node.js dependencies.
 */

export interface TelemetryClientConfig {
  /** Product identifier (e.g. 'chronomind', 'nomgap', 'lysnrai'). */
  productId: string;

  /** Platform-service base URL or telemetry ingest endpoint base. */
  baseUrl: string;

  /** Endpoint path appended to baseUrl. Default: '/telemetry/events'. */
  endpoint?: string;

  /** Platform identifier (e.g. 'web', 'mobile', 'desktop'). */
  platform: string;

  /** Channel identifier (e.g. 'pwa', 'react_native', 'web_app'). */
  channel: string;

  /** Transport: 'beacon' uses sendBeacon (web), 'fetch' uses fetch (RN/fallback). Default: 'fetch'. */
  transport?: 'beacon' | 'fetch';

  /** Max events to queue before auto-flush. Default: 50. */
  maxQueue?: number;

  /** Flush interval in milliseconds. Default: 30000. */
  flushIntervalMs?: number;

  /** App version string. Default: '0.0.0'. */
  appVersion?: string;

  /** Build number. Default: '0'. */
  buildNumber?: string;

  /** Release channel. Default: 'dev'. */
  releaseChannel?: string;

  /** OS family. Default: 'other'. */
  osFamily?: string;

  /** OS version. Default: ''. */
  osVersion?: string;

  /** Storage adapter for install ID persistence. Uses localStorage by default. */
  storage?: TelemetryStorage;
}

export interface TelemetryStorage {
  getItem(key: string): string | null;
  setItem(key: string, value: string): void;
}

export interface TelemetryEvent {
  id: string;
  productId: string;
  userId?: string;
  anonymousInstallId: string;
  sessionId: string;
  platform: string;
  channel: string;
  osFamily: string;
  osVersion: string;
  appVersion: string;
  buildNumber: string;
  releaseChannel: string;
  eventType: string;
  module: string;
  eventName: string;
  feature?: string;
  message?: string;
  tags?: Record<string, string>;
  metrics?: Record<string, number>;
  occurredAt: string;
}

export interface TelemetryClient {
  /** Initialize the telemetry client and start periodic flushing. */
  init(): void;

  /** Track a telemetry event. */
  trackEvent(
    eventType: string,
    module: string,
    eventName: string,
    extra?: {
      feature?: string;
      message?: string;
      tags?: Record<string, string>;
      metrics?: Record<string, number>;
      userId?: string;
    }
  ): void;

  /** Flush all queued events immediately. */
  flush(): void;

  /** Stop the periodic flush timer and flush remaining events. */
  shutdown(): void;

  /** Get the anonymous install ID. */
  getInstallId(): string;

  /** Get the current session ID. */
  getSessionId(): string;
}
