/**
 * Core Events
 * 
 * Event names, event payloads, and event logging interfaces.
 * All event payloads include schemaVersion for future-proofing.
 * 
 * @author Matthew Raymer
 * @version 1.0.0
 */

/**
 * Event log entry
 * 
 * Structured log entry for observability and debugging
 */
export interface EventLog {
  /** Unique event identifier */
  id: string;
  /** Timestamp when event occurred (milliseconds since epoch) */
  timestamp: number;
  /** Log level */
  level: 'INFO' | 'WARN' | 'ERROR';
  /** Event code (e.g., 'DNP-FETCH-START') */
  eventCode: string;
  /** Human-readable message */
  message: string;
  /** Optional event data */
  data?: Record<string, unknown>;
  /** Optional duration in milliseconds */
  duration?: number;
  /** Schema version for future-proofing */
  schemaVersion: number;
}

/**
 * Event code constants
 * 
 * Stable event codes for observability and logging
 */
export const EVENT_CODES = {
  FETCH_START: 'DNP-FETCH-START',
  FETCH_SUCCESS: 'DNP-FETCH-SUCCESS',
  FETCH_FAILURE: 'DNP-FETCH-FAILURE',
  FETCH_RETRY: 'DNP-FETCH-RETRY',
  NOTIFY_START: 'DNP-NOTIFY-START',
  NOTIFY_SUCCESS: 'DNP-NOTIFY-SUCCESS',
  NOTIFY_FAILURE: 'DNP-NOTIFY-FAILURE',
  NOTIFY_SKIPPED_TTL: 'DNP-NOTIFY-SKIPPED-TTL',
  CALLBACK_START: 'DNP-CB-START',
  CALLBACK_SUCCESS: 'DNP-CB-SUCCESS',
  CALLBACK_FAILURE: 'DNP-CB-FAILURE',
  CALLBACK_RETRY: 'DNP-CB-RETRY',
  CALLBACK_CIRCUIT_OPEN: 'DNP-CB-CIRCUIT-OPEN',
  CALLBACK_CIRCUIT_CLOSE: 'DNP-CB-CIRCUIT-CLOSE',
  BOOT_RECOVERY: 'DNP-BOOT-RECOVERY',
  SCHEDULE_UPDATE: 'DNP-SCHEDULE-UPDATE',
  CACHE_HIT: 'DNP-CACHE-HIT',
  CACHE_MISS: 'DNP-CACHE-MISS',
  TTL_EXPIRED: 'DNP-TTL-EXPIRED',
  METRICS_RESET: 'DNP-METRICS-RESET',
  LOGS_COMPACTED: 'DNP-LOGS-COMPACTED',
  // User interaction events
  USER_OPT_OUT: 'DNP-USER-OPT-OUT',
  USER_OPT_IN: 'DNP-USER-OPT-IN',
  PERMISSION_DENIED: 'DNP-PERMISSION-DENIED',
  PERMISSION_GRANTED: 'DNP-PERMISSION-GRANTED',
  // Rate limiting events
  RATE_LIMIT_HIT: 'DNP-RATE-LIMIT-HIT',
  RATE_LIMIT_RESET: 'DNP-RATE-LIMIT-RESET',
  BACKOFF_START: 'DNP-BACKOFF-START',
  BACKOFF_END: 'DNP-BACKOFF-END',
  // Storage events
  STORAGE_FULL: 'DNP-STORAGE-FULL',
  STORAGE_CLEANUP: 'DNP-STORAGE-CLEANUP',
  // Platform-specific events
  ANDROID_WORKMANAGER_START: 'DNP-ANDROID-WM-START',
  IOS_BGTASK_START: 'DNP-IOS-BGTASK-START',
  ELECTRON_NOTIFICATION: 'DNP-ELECTRON-NOTIFICATION',
  // Recovery events
  RECOVERY_START: 'DNP-RECOVERY-START',
  RECOVERY_COMPLETE: 'DNP-RECOVERY-COMPLETE',
  RECOVERY_ERROR: 'DNP-RECOVERY-ERROR',
  // Database events
  DB_QUERY_START: 'DNP-DB-QUERY-START',
  DB_QUERY_COMPLETE: 'DNP-DB-QUERY-COMPLETE',
  DB_QUERY_ERROR: 'DNP-DB-QUERY-ERROR',
  // State transition events
  STATE_TRANSITION: 'DNP-STATE-TRANSITION',
  // Background task events
  BACKGROUND_TASK_START: 'DNP-BG-TASK-START',
  BACKGROUND_TASK_COMPLETE: 'DNP-BG-TASK-COMPLETE',
  BACKGROUND_TASK_ERROR: 'DNP-BG-TASK-ERROR',
} as const;

/**
 * Current schema version for event payloads
 */
export const EVENT_SCHEMA_VERSION = 1;

/**
 * Create an event log entry with schema version
 */
export function createEventLog(
  level: 'INFO' | 'WARN' | 'ERROR',
  eventCode: string,
  message: string,
  data?: Record<string, unknown>,
  duration?: number,
  id?: string
): EventLog {
  return {
    id: id || `${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
    timestamp: Date.now(),
    level,
    eventCode,
    message,
    data,
    duration,
    schemaVersion: EVENT_SCHEMA_VERSION,
  };
}

