import { Hono } from 'hono';
import type { IServiceContainer } from '@stock-bot/handlers';
import { BacktestService } from '../services/backtest.service';
import { getLogger } from '@stock-bot/logger';

const logger = getLogger('backtest-routes');

export function createBacktestRoutes(container: IServiceContainer) {
  const backtestRoutes = new Hono();
  const backtestService = new BacktestService(container);

  // Create a new backtest
  backtestRoutes.post('/api/backtests', async (c) => {
    try {
      const body = await c.req.json();
      
      // Validate required fields
      if (!body.strategy || !body.symbols || !body.startDate || !body.endDate) {
        return c.json({ 
          error: 'Missing required fields: strategy, symbols, startDate, endDate' 
        }, 400);
      }

      const backtest = await backtestService.createBacktest({
        ...body,
        initialCapital: body.initialCapital || 100000,
      });
      
      return c.json(backtest, 201);
    } catch (error) {
      logger.error('Failed to create backtest', { error });
      return c.json({ error: 'Failed to create backtest' }, 500);
    }
  });

  // Get backtest status
  backtestRoutes.get('/api/backtests/:id', async (c) => {
    try {
      const id = c.req.param('id');
      const backtest = await backtestService.getBacktest(id);
      
      if (!backtest) {
        return c.json({ error: 'Backtest not found' }, 404);
      }
      
      return c.json(backtest);
    } catch (error) {
      logger.error('Failed to get backtest', { error });
      return c.json({ error: 'Failed to get backtest' }, 500);
    }
  });

  // Get backtest results
  backtestRoutes.get('/api/backtests/:id/results', async (c) => {
    try {
      const id = c.req.param('id');
      const results = await backtestService.getBacktestResults(id);
      
      if (!results) {
        return c.json({ error: 'Results not found' }, 404);
      }
      
      return c.json(results);
    } catch (error) {
      logger.error('Failed to get results', { error });
      return c.json({ error: 'Failed to get results' }, 500);
    }
  });

  // List all backtests
  backtestRoutes.get('/api/backtests', async (c) => {
    try {
      const limit = Number(c.req.query('limit')) || 50;
      const offset = Number(c.req.query('offset')) || 0;
      
      const backtests = await backtestService.listBacktests({ limit, offset });
      return c.json(backtests);
    } catch (error) {
      logger.error('Failed to list backtests', { error });
      return c.json({ error: 'Failed to list backtests' }, 500);
    }
  });

  // Cancel a backtest
  backtestRoutes.post('/api/backtests/:id/cancel', async (c) => {
    try {
      const id = c.req.param('id');
      await backtestService.cancelBacktest(id);
      return c.json({ message: 'Backtest cancelled' });
    } catch (error) {
      logger.error('Failed to cancel backtest', { error });
      return c.json({ error: 'Failed to cancel backtest' }, 500);
    }
  });

  return backtestRoutes;
}