/**
 * Service Registry Configuration
 * Maps services to their Redis databases and configurations
 */

export interface ServiceConfig {
  /** Redis database number for this service (used for both queues and cache) */
  db: number;
  /** Prefix for queue keys (e.g., 'bull:di') */
  queuePrefix: string;
  /** Prefix for cache keys (e.g., 'cache:di') */
  cachePrefix: string;
  /** Whether this service only produces jobs (doesn't process them) */
  producerOnly?: boolean;
  /** List of handlers this service owns (auto-discovered if not provided) */
  handlers?: string[];
}

/**
 * Central registry of all services and their configurations
 * Each service gets one Redis DB for both queues and cache
 * 
 * Database assignments:
 * - db:0 = Global shared cache
 * - db:1 = data-ingestion (queues + cache)
 * - db:2 = data-pipeline (queues + cache)
 * - db:3 = web-api (cache only, producer-only for queues)
 */
export const SERVICE_REGISTRY: Record<string, ServiceConfig> = {
  'data-ingestion': {
    db: 1,
    queuePrefix: 'bull:di',
    cachePrefix: 'cache:di',
    handlers: ['ceo', 'qm', 'webshare', 'ib', 'proxy'],
  },
  'data-pipeline': {
    db: 2,
    queuePrefix: 'bull:dp',
    cachePrefix: 'cache:dp',
    handlers: ['exchanges', 'symbols'],
  },
  'web-api': {
    db: 3,
    queuePrefix: 'bull:api',  // Not used since producer-only
    cachePrefix: 'cache:api',
    producerOnly: true,
  },
  // Add aliases for services with different naming conventions
  'webApi': {
    db: 3,
    queuePrefix: 'bull:api',
    cachePrefix: 'cache:api',
    producerOnly: true,
  },
  'dataIngestion': {
    db: 1,
    queuePrefix: 'bull:di',
    cachePrefix: 'cache:di',
    handlers: ['ceo', 'qm', 'webshare', 'ib', 'proxy'],
  },
  'dataPipeline': {
    db: 2,
    queuePrefix: 'bull:dp',
    cachePrefix: 'cache:dp',
    handlers: ['exchanges', 'symbols'],
  },
};

/**
 * Get service configuration
 */
export function getServiceConfig(serviceName: string): ServiceConfig | undefined {
  return SERVICE_REGISTRY[serviceName];
}

/**
 * Find which service owns a handler
 */
export function findServiceForHandler(handlerName: string): string | undefined {
  for (const [serviceName, config] of Object.entries(SERVICE_REGISTRY)) {
    if (config.handlers?.includes(handlerName)) {
      return serviceName;
    }
  }
  return undefined;
}

/**
 * Get full queue name - just the handler name since each service has its own Redis DB
 */
export function getFullQueueName(serviceName: string, handlerName: string): string {
  // Just return the handler name since DB isolation provides namespace separation
  return handlerName;
}

/**
 * Parse a full queue name into service and handler
 * Since queue names are just handler names now, we need to find the service from the handler
 */
export function parseQueueName(fullQueueName: string): { service: string; handler: string } | null {
  // Queue name is just the handler name now
  const handlerName = fullQueueName;
  
  // Find which service owns this handler
  const serviceName = findServiceForHandler(handlerName);
  
  if (!serviceName) {
    return null;
  }
  
  return {
    service: serviceName,
    handler: handlerName,
  };
}