import type { AttributionData, DeviceData } from './common';

/**
 * Base event interface - all events extend this
 */
export interface BaseEvent {
  /** Event type identifier */
  type: string;
  /** ISO timestamp of when event occurred */
  timestamp: string;
  /** Session identifier */
  sessionId: string;
  /** Hashed visitor identifier (not persistent across sessions) */
  visitorId?: string;
  /** User ID if authenticated */
  userId?: string;
  /** Arbitrary metadata */
  properties?: Record<string, unknown>;
}

/**
 * Page view event
 */
export interface PageViewEvent extends BaseEvent {
  type: 'page_view';
  properties: {
    url: string;
    title?: string;
    referrer?: string;
    path: string;
    queryParams?: Record<string, string>;
  };
}

/**
 * Click event
 */
export interface ClickEvent extends BaseEvent {
  type: 'click';
  properties: {
    elementId?: string;
    elementClass?: string;
    elementTag: string;
    elementText?: string;
    href?: string;
    x: number;
    y: number;
  };
}

/**
 * Form submission event
 */
export interface FormSubmitEvent extends BaseEvent {
  type: 'form_submit';
  properties: {
    formId?: string;
    formName?: string;
    formAction?: string;
    fieldCount: number;
  };
}

/**
 * Custom event (user-defined)
 */
export interface CustomEvent extends BaseEvent {
  type: 'custom';
  properties: {
    name: string;
    category?: string;
    label?: string;
    value?: number;
    [key: string]: unknown;
  };
}

/**
 * Conversion event (goal completion)
 */
export interface ConversionEvent extends BaseEvent {
  type: 'conversion';
  properties: {
    goalId: string;
    goalName: string;
    value?: number;
    currency?: string;
  };
}

/**
 * Funnel step event
 */
export interface FunnelStepEvent extends BaseEvent {
  type: 'funnel_step';
  properties: {
    funnelId: string;
    stepId: string;
    stepNumber: number;
    stepName: string;
  };
}

/**
 * Revenue event (transaction)
 */
export interface RevenueEvent extends BaseEvent {
  type: 'revenue';
  properties: {
    transactionId: string;
    amount: number;
    currency: string;
    productIds?: string[];
    category?: string;
  };
}

/**
 * Error event
 */
export interface ErrorEvent extends BaseEvent {
  type: 'error';
  properties: {
    message: string;
    stack?: string;
    source?: string;
    line?: number;
    column?: number;
  };
}

/**
 * Session start event
 */
export interface SessionStartEvent extends BaseEvent {
  type: 'session_start';
  properties: {
    isNewVisitor: boolean;
    landingPage: string;
  };
}

/**
 * Session end event
 */
export interface SessionEndEvent extends BaseEvent {
  type: 'session_end';
  properties: {
    duration: number;
    pageViews: number;
    events: number;
  };
}

/**
 * Union type of all event types
 */
export type AnalyticsEvent =
  | PageViewEvent
  | ClickEvent
  | FormSubmitEvent
  | CustomEvent
  | ConversionEvent
  | FunnelStepEvent
  | RevenueEvent
  | ErrorEvent
  | SessionStartEvent
  | SessionEndEvent;

/**
 * Event payload sent to collector
 */
export interface TrackEventPayload {
  events: AnalyticsEvent[];
  device?: DeviceData;
  attribution?: AttributionData;
}

/**
 * Event type string literals
 */
export type EventType = AnalyticsEvent['type'];

/**
 * Event properties by type
 */
export type EventProperties<T extends EventType> = Extract<
  AnalyticsEvent,
  { type: T }
>['properties'];
