/**
 * EventBus Listener Registry
 *
 * Registers all event listeners at application startup.
 * Each listener is independent — if one fails to register, others continue.
 */

import { logger } from '../../utils/logger';
import { registerListmonkListener } from './listmonk.listener';
import { registerRocketChatListener } from './rocketchat.listener';
import { registerCrmActivityListener } from './crm-activity.listener';
import { registerCalendarSyncListener } from './calendar-sync.listener';
import { registerN8nWebhookListener } from './n8n-webhook.listener';
import { registerGancioListener } from './gancio.listener';
import { registerEngagementScoringListener } from './engagement-scoring.listener';
import { registerHomepageStatsListener } from './homepage-stats.listener';

export function registerAllEventListeners(): void {
  const listeners = [
    { name: 'Listmonk', register: registerListmonkListener },
    { name: 'Rocket.Chat', register: registerRocketChatListener },
    { name: 'CRM Activity', register: registerCrmActivityListener },
    { name: 'Calendar Sync', register: registerCalendarSyncListener },
    { name: 'n8n Webhook', register: registerN8nWebhookListener },
    { name: 'Gancio', register: registerGancioListener },
    { name: 'Engagement Scoring', register: registerEngagementScoringListener },
    { name: 'Homepage Stats', register: registerHomepageStatsListener },
  ];

  let registered = 0;
  for (const listener of listeners) {
    try {
      listener.register();
      registered++;
    } catch (err) {
      logger.warn(`EventBus: failed to register ${listener.name} listener:`, err);
    }
  }

  logger.info(`EventBus: ${registered}/${listeners.length} listeners registered`);
}
