import { BacktestEngine } from '../src/backtest/BacktestEngine';
import { StrategyManager } from '../src/strategies/StrategyManager';
import { StorageService } from '../src/services/StorageService';
import { ModeManager } from '../src/core/ModeManager';
import { MarketDataService } from '../src/services/MarketDataService';
import { ExecutionService } from '../src/services/ExecutionService';
import { IServiceContainer } from '@stock-bot/di';
import { getLogger } from '@stock-bot/logger';

const logger = getLogger('BacktestTest');

async function runSimpleBacktest() {
  // Create service container
  const container: IServiceContainer = {
    logger: {
      info: (msg: string, ...args: any[]) => console.log('[INFO]', msg, ...args),
      error: (msg: string, ...args: any[]) => console.error('[ERROR]', msg, ...args),
      warn: (msg: string, ...args: any[]) => console.warn('[WARN]', msg, ...args),
      debug: (msg: string, ...args: any[]) => console.log('[DEBUG]', msg, ...args),
    } as any,
    custom: {}
  };

  // Initialize services
  const storageService = new StorageService();
  const marketDataService = new MarketDataService(container);
  const executionService = new ExecutionService(container);
  const modeManager = new ModeManager(container, marketDataService, executionService, storageService);
  const strategyManager = new StrategyManager(container);

  // Set services in container
  container.custom = {
    MarketDataService: marketDataService,
    ExecutionService: executionService,
    ModeManager: modeManager,
    StorageService: storageService
  };

  // Initialize backtest mode with full config
  await modeManager.initializeMode({
    mode: 'backtest',
    startDate: '2023-01-01T00:00:00Z',
    endDate: '2024-01-01T00:00:00Z',
    speed: 'max',
    symbols: ['AAPL'],
    initialCapital: 100000,
    dataFrequency: '1d',
    strategy: 'sma-crossover'
  });

  // Create backtest engine
  const backtestEngine = new BacktestEngine(container, storageService, strategyManager);

  // Configure backtest - shorter period for faster testing
  const config = {
    mode: 'backtest' as const,  // Add mode field
    name: 'SMA Crossover Test',
    strategy: 'sma-crossover',
    symbols: ['AAPL'],  // Just one symbol for simplicity
    startDate: '2023-01-01T00:00:00Z',  // ISO datetime format
    endDate: '2024-01-01T00:00:00Z',  // ISO datetime format
    initialCapital: 100000,
    commission: 0.001,
    slippage: 0.0001,
    dataFrequency: '1d' as const,
    speed: 'max' as const
  };

  console.log('Starting backtest with configuration:', config);

  try {
    const result = await backtestEngine.runBacktest(config);
    
    console.log('\n=== BACKTEST RESULTS ===');
    console.log(`Total Trades: ${result.metrics.totalTrades}`);
    console.log(`Win Rate: ${result.metrics.winRate.toFixed(2)}%`);
    console.log(`Total Return: ${result.metrics.totalReturn.toFixed(2)}%`);
    console.log(`Sharpe Ratio: ${result.metrics.sharpeRatio.toFixed(2)}`);
    console.log(`Max Drawdown: ${result.metrics.maxDrawdown.toFixed(2)}%`);
    
    console.log('\n=== TRADE HISTORY ===');
    if (result.trades.length === 0) {
      console.log('No trades were executed!');
    } else {
      result.trades.forEach((trade, i) => {
        console.log(`\nTrade ${i + 1}:`);
        console.log(`  Symbol: ${trade.symbol}`);
        console.log(`  Entry: ${trade.entryDate} @ $${trade.entryPrice.toFixed(2)}`);
        console.log(`  Exit: ${trade.exitDate} @ $${trade.exitPrice.toFixed(2)}`);
        console.log(`  P&L: $${trade.pnl.toFixed(2)} (${trade.pnlPercent.toFixed(2)}%)`);
      });
    }

    // Show some equity curve data
    console.log('\n=== EQUITY CURVE (first and last 5 points) ===');
    const equityCurve = result.equity;
    if (equityCurve.length > 0) {
      equityCurve.slice(0, 5).forEach(point => {
        console.log(`${point.date}: $${point.value.toFixed(2)}`);
      });
      if (equityCurve.length > 10) {
        console.log('...');
        equityCurve.slice(-5).forEach(point => {
          console.log(`${point.date}: $${point.value.toFixed(2)}`);
        });
      }
    }

  } catch (error) {
    console.error('Backtest failed:', error);
  }
}

// Run the test
runSimpleBacktest().catch(console.error);