/**
 * Queue Metrics Collection Initializer
 * Collects BullMQ queue metrics and exposes them to Prometheus
 */

import { tatQueue } from '../queues/tatQueue';
import { pauseResumeQueue } from '../queues/pauseResumeQueue';
import { startQueueMetricsCollection } from '../middlewares/metrics.middleware';
import logger from './logger';

let metricsCollectionInterval: NodeJS.Timeout | null = null;

/**
 * Initialize queue metrics collection
 * Should be called after queues are initialized
 */
export function initializeQueueMetrics(): void {
  try {
    const queues: { name: string; queue: any }[] = [];

    if (tatQueue) {
      queues.push({ name: 'tatQueue', queue: tatQueue });
    }

    if (pauseResumeQueue) {
      queues.push({ name: 'pauseResumeQueue', queue: pauseResumeQueue });
    }

    if (queues.length === 0) {
      logger.warn('[Queue Metrics] No queues available for metrics collection');
      return;
    }

    // Start collecting metrics every 15 seconds
    metricsCollectionInterval = startQueueMetricsCollection(queues, 15000);
    
    logger.info(`[Queue Metrics] ✅ Started metrics collection for ${queues.length} queue(s)`);
  } catch (error) {
    logger.error('[Queue Metrics] Failed to initialize:', error);
  }
}

/**
 * Stop queue metrics collection
 */
export function stopQueueMetrics(): void {
  if (metricsCollectionInterval) {
    clearInterval(metricsCollectionInterval);
    metricsCollectionInterval = null;
    logger.info('[Queue Metrics] Stopped metrics collection');
  }
}

