/**
 * Data Ingestion Service
 * Simplified entry point using ServiceApplication framework
 */

import { ServiceApplication } from '@stock-bot/di';
import { getLogger } from '@stock-bot/logger';
import { initializeStockConfig, type StockAppConfig } from '@stock-bot/stock-config';
import { createRoutes } from './routes/create-routes';
// Local imports
import { initializeAllHandlers } from './handlers';

// Initialize configuration with service-specific overrides
const config = initializeStockConfig('dataIngestion');

// Log the full configuration
const logger = getLogger('data-ingestion');
logger.info('Service configuration:', config);

// Create service application
const app = new ServiceApplication(
  config,
  {
    serviceName: 'data-ingestion',
    enableHandlers: true,
    enableScheduledJobs: true,
    corsConfig: {
      origin: '*',
      allowMethods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS', 'PATCH'],
      allowHeaders: ['Content-Type', 'Authorization'],
      credentials: false,
    },
    serviceMetadata: {
      version: '1.0.0',
      description: 'Market data ingestion from multiple providers',
      endpoints: {
        health: '/health',
        handlers: '/api/handlers',
      },
    },
  },
  {
    // Lifecycle hooks if needed
    onStarted: _port => {
      const logger = getLogger('data-ingestion');
      logger.info('Data ingestion service startup initiated with ServiceApplication framework');
    },
  }
);

// Container factory function
async function createContainer(config: StockAppConfig) {
  const { ServiceContainerBuilder } = await import('@stock-bot/di');

  const container = await new ServiceContainerBuilder()
    .withConfig(config)
    .withOptions({
      enableQuestDB: false, // Data ingestion doesn't need QuestDB yet
      enableMongoDB: true,
      enablePostgres: config.database?.postgres?.enabled ?? false,
      enableCache: true,
      enableQueue: true,
      enableBrowser: true, // Data ingestion needs browser for web scraping
      enableProxy: true, // Data ingestion needs proxy for rate limiting
    })
    .build(); // This automatically initializes services

  return container;
}

// Start the service
app.start(createContainer, createRoutes, initializeAllHandlers).catch(error => {
  const logger = getLogger('data-ingestion');
  logger.fatal('Failed to start data service', { error });
  process.exit(1);
});
