export interface EventBusMessage<T = any> {
  id: string;
  type: string;
  source: string;
  timestamp: number;
  data: T;
  metadata?: Record<string, unknown>;
}

export interface EventHandler<T = any> {
  (message: EventBusMessage<T>): Promise<void> | void;
}

export interface EventBusConfig {
  serviceName: string;
  redisConfig: {
    host: string;
    port: number;
    password?: string;
    db?: number;
  };
  enableLogging?: boolean;
}

export interface EventSubscription {
  channel: string;
  handler: EventHandler;
}

// Trading-specific event types
export enum TradingEventType {
  // Market data events
  PRICE_UPDATE = 'market.price.update',
  ORDERBOOK_UPDATE = 'market.orderbook.update',
  TRADE_EXECUTED = 'market.trade.executed',

  // Order events
  ORDER_CREATED = 'order.created',
  ORDER_FILLED = 'order.filled',
  ORDER_CANCELLED = 'order.cancelled',
  ORDER_REJECTED = 'order.rejected',

  // Position events
  POSITION_OPENED = 'position.opened',
  POSITION_CLOSED = 'position.closed',
  POSITION_UPDATED = 'position.updated',

  // Strategy events
  STRATEGY_SIGNAL = 'strategy.signal',
  STRATEGY_STARTED = 'strategy.started',
  STRATEGY_STOPPED = 'strategy.stopped',

  // Risk events
  RISK_LIMIT_BREACH = 'risk.limit.breach',
  RISK_WARNING = 'risk.warning',

  // System events
  SERVICE_STARTED = 'system.service.started',
  SERVICE_STOPPED = 'system.service.stopped',
  SERVICE_ERROR = 'system.service.error',
}

// Event data types
export interface PriceUpdateEvent {
  symbol: string;
  price: number;
  volume: number;
  timestamp: number;
}

export interface OrderEvent {
  orderId: string;
  symbol: string;
  side: 'buy' | 'sell';
  quantity: number;
  price?: number;
  type: 'market' | 'limit' | 'stop' | 'stop_limit';
  status: string;
  portfolioId: string;
  strategyId?: string;
}

export interface PositionEvent {
  positionId: string;
  symbol: string;
  quantity: number;
  averageCost: number;
  currentPrice: number;
  unrealizedPnl: number;
  realizedPnl: number;
  portfolioId: string;
}

export interface StrategySignalEvent {
  strategyId: string;
  signal: 'buy' | 'sell' | 'hold';
  symbol: string;
  confidence: number;
  indicators: Record<string, number>;
  timestamp: number;
}

export interface RiskEvent {
  type: 'position_size' | 'daily_loss' | 'max_drawdown' | 'concentration';
  severity: 'warning' | 'critical';
  currentValue: number;
  limit: number;
  portfolioId?: string;
  strategyId?: string;
  message: string;
}
