/**
 * Handler auto-registration for data pipeline service
 * Automatically discovers and registers all handlers
 */

import type { IServiceContainer } from '@stock-bot/handlers';
import { autoRegisterHandlers } from '@stock-bot/handlers';
import { getLogger } from '@stock-bot/logger';

// Import handlers for bundling (ensures they're included in the build)
import './exchanges/exchanges.handler';
import './symbols/symbols.handler';

const logger = getLogger('pipeline-handler-init');

/**
 * Initialize and register all handlers automatically
 */
export async function initializeAllHandlers(container: IServiceContainer): Promise<void> {
  logger.info('Initializing data pipeline handlers...');
  
  try {
    // Auto-register all handlers in this directory
    const result = await autoRegisterHandlers(__dirname, container, {
      pattern: '.handler.',
      exclude: ['test', 'spec', '.old'],
      dryRun: false,
    });

    logger.info('Handler auto-registration complete', {
      registered: result.registered,
      failed: result.failed,
    });

    if (result.failed.length > 0) {
      logger.error('Some handlers failed to register', { failed: result.failed });
    }
  } catch (error) {
    logger.error('Handler auto-registration failed', { error });
    throw error;
  }
}