/**
 * Route factory for web API service
 * Creates routes with access to the service container
 */

import { Hono } from 'hono';
import { cors } from 'hono/cors';
import type { IServiceContainer } from '@stock-bot/handlers';
import { createExchangeRoutes } from './exchange.routes';
import { createHealthRoutes } from './health.routes';
import { createMonitoringRoutes } from './monitoring.routes';
import { createPipelineRoutes } from './pipeline.routes';
import { createBacktestRoutes } from './backtest.routes';
import { createBacktestV2Routes } from './backtests-v2.routes';

export function createRoutes(container: IServiceContainer): Hono {
  const app = new Hono();
  
  // Add CORS middleware
  app.use('*', cors({
    origin: ['http://localhost:4200', 'http://localhost:3000', 'http://localhost:3002', 'http://localhost:5173', 'http://localhost:5174'],
    allowMethods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'],
    allowHeaders: ['Content-Type', 'Authorization'],
    credentials: true,
  }));

  // Create routes with container
  const healthRoutes = createHealthRoutes(container);
  const exchangeRoutes = createExchangeRoutes(container);
  const monitoringRoutes = createMonitoringRoutes(container);
  const pipelineRoutes = createPipelineRoutes(container);
  const backtestRoutes = createBacktestRoutes(container);
  const backtestV2Routes = createBacktestV2Routes(container);

  // Mount routes
  app.route('/health', healthRoutes);
  app.route('/api/exchanges', exchangeRoutes);
  app.route('/api/system/monitoring', monitoringRoutes);
  app.route('/api/pipeline', pipelineRoutes);
  app.route('/', backtestRoutes); // Mounted at root since routes already have /api prefix
  app.route('/', backtestV2Routes); // V2 routes also mounted at root

  return app;
}
