import { eventBus } from '../eventBus';
import { pushToMerchant, broadcastToAll } from './server';

// 绑定事件到WebSocket推送
export const bindEventToWebSocket = (): void => {
  console.log('[WebSocket] Binding events to WebSocket...');
  
  // 任务相关事件
  eventBus.on('JOB_CREATED', (event) => {
    pushToMerchant(event.merchantId || 'anonymous', {
      type: 'JOB_CREATED',
      data: event
    });
  });
  
  eventBus.on('JOB_UPDATED', (event) => {
    pushToMerchant(event.merchantId || 'anonymous', {
      type: 'JOB_UPDATED',
      data: event
    });
  });
  
  eventBus.on('JOB_COMPLETED', (event) => {
    pushToMerchant(event.merchantId || 'anonymous', {
      type: 'JOB_COMPLETED',
      data: event
    });
  });
  
  eventBus.on('JOB_FAILED', (event) => {
    pushToMerchant(event.merchantId || 'anonymous', {
      type: 'JOB_FAILED',
      data: event
    });
  });
  
  // AI相关事件
  eventBus.on('AI_TASK_COMPLETED', (event) => {
    pushToMerchant(event.merchantId || 'anonymous', {
      type: 'AI_TASK_COMPLETED',
      data: event
    });
  });
  
  // 计费相关事件
  eventBus.on('BILLING_GENERATED', (event) => {
    pushToMerchant(event.merchantId || 'anonymous', {
      type: 'BILLING_GENERATED',
      data: event
    });
  });
  
  // 库存相关事件
  eventBus.on('INVENTORY_UPDATED', (event) => {
    broadcastToAll({
      type: 'INVENTORY_UPDATED',
      data: event
    });
  });
  
  console.log('[WebSocket] Events bound to WebSocket successfully');
};

export default {
  bindEventToWebSocket
};
