/**
 * Product analytics tracker types (spec §8.2).
 * Events are plain JSON objects validated against the shared taxonomy.
 */
import type { ProductAnalyticsEventName } from "@app/shared-types";

export interface AnalyticsEvent {
  name: ProductAnalyticsEventName;
  /** ISO 8601 timestamp. Defaults to now if omitted. */
  occurredAt?: string;
  /** UUID or device-stable pseudonymous id. */
  anonymousId?: string;
  /** Session id, rotated periodically. */
  sessionId?: string;
  /** Active household when event was produced. */
  householdId?: string;
  appVersion?: string;
  platform?: "ios" | "android" | "web";
  locale?: string;
  experimentVariant?: string;
  /** Structured, safe properties only (no PII, no free text). */
  properties?: Record<string, unknown>;
}

export interface AnalyticsBatchPayload {
  events: AnalyticsEvent[];
}

export interface AnalyticsBatchResult {
  accepted: number;
  rejected?: number;
  errors?: Array<{ index: number; message: string }>;
}

export interface TrackerConfig {
  apiBaseUrl: string;
  /** Auth token; events sent before login are anonymous. */
  authToken?: string;
  /** Anonymous id persisted for this install. */
  anonymousId: string;
  /** Current session id. */
  sessionId?: string;
  appVersion: string;
  platform: "ios" | "android" | "web";
  locale: string;
  /** Max events per batch. */
  maxBatchSize?: number;
  /** Flush interval in ms. */
  flushIntervalMs?: number;
}
