/**
 * System-wide event definitions for type safety
 */
export interface SystemEvents {
  // User events
  'user.registered': {
    userId: string;
    user: {
      id: string;
      email: string;
      name: string;
      role: string;
    };
  };
  
  'user.login': {
    userId: string;
    timestamp: Date;
    sessionId: string;
  };
  
  'user.logout': {
    userId: string;
    sessionId: string;
    duration: number; // in seconds
  };

  // Course events
  'course.enrolled': {
    userId: string;
    courseId: string;
    enrollmentDate: Date;
  };
  
  'course.completed': {
    userId: string;
    courseId: string;
    completionDate: Date;
    score?: number;
  };
  
  'course.progress': {
    userId: string;
    courseId: string;
    progress: number; // 0-100
    lastActivity: Date;
  };

  // Exam events
  'exam.started': {
    userId: string;
    examId: string;
    sessionId: string;
    startTime: Date;
  };
  
  'exam.completed': {
    userId: string;
    examId: string;
    sessionId: string;
    result: {
      score: number;
      passed: boolean;
      answers: any[];
      duration: number;
    };
  };
  
  'exam.submitted': {
    userId: string;
    examId: string;
    sessionId: string;
    submissionTime: Date;
  };

  // Learning activity events
  'learning.session.started': {
    userId: string;
    sessionId: string;
    courseId?: string;
    startTime: Date;
  };
  
  'learning.session.ended': {
    userId: string;
    sessionId: string;
    courseId?: string;
    duration: number; // in seconds
    endTime: Date;
  };
  
  'learning.milestone.reached': {
    userId: string;
    milestoneId: string;
    milestoneType: 'hours' | 'courses' | 'exams' | 'streak';
    value: number;
    achievedAt: Date;
  };

  // Certificate events
  'certificate.issued': {
    userId: string;
    certificateId: string;
    courseId: string;
    issuedAt: Date;
  };

  // Plugin/Feature events
  'plugin.activated': {
    pluginId: string;
    version: string;
    activatedAt: Date;
  };
  
  'plugin.deactivated': {
    pluginId: string;
    deactivatedAt: Date;
  };
  
  'feature.enabled': {
    featureId: string;
    userId?: string; // if user-specific
    enabledAt: Date;
  };
  
  'feature.disabled': {
    featureId: string;
    userId?: string;
    disabledAt: Date;
  };

  // System events
  'system.error': {
    error: Error;
    context: string;
    userId?: string;
    timestamp: Date;
  };
  
  'system.maintenance.started': {
    startTime: Date;
    estimatedDuration?: number;
    message?: string;
  };
  
  'system.maintenance.ended': {
    endTime: Date;
    actualDuration: number;
  };
}

/**
 * Type-safe event emitter
 */
export interface TypedEventBus {
  emit<K extends keyof SystemEvents>(event: K, data: SystemEvents[K]): Promise<void>;
  on<K extends keyof SystemEvents>(event: K, handler: (data: SystemEvents[K]) => void | Promise<void>): { unsubscribe(): void };
  once<K extends keyof SystemEvents>(event: K, handler: (data: SystemEvents[K]) => void | Promise<void>): { unsubscribe(): void };
  off<K extends keyof SystemEvents>(event: K, handler: (data: SystemEvents[K]) => void | Promise<void>): void;
}