/**
 * Examples of modifying existing functions to only work with symbol 'X'
 */

import { OperationTracker } from '../src/shared/operation-manager';

// Example 1: Modified getStaleSymbols to only return symbol X
async function getStaleSymbolsOnlyX(
  operationTracker: OperationTracker,
  providerName: string,
  operationName: string,
  options: any = {}
) {
  // Method 1: Add symbolFilter to options
  const modifiedOptions = {
    ...options,
    symbolFilter: { symbol: 'X' }
  };
  
  return operationTracker.getStaleSymbols(providerName, operationName, modifiedOptions);
}

// Example 2: Modified sophisticated backtest function for symbol X only
async function runSophisticatedBacktestOnlyX(orchestrator: any) {
  const symbols = ['X']; // Only test symbol X
  
  const backtestConfig = {
    symbols,
    startDate: new Date('2023-01-01'),
    endDate: new Date('2024-01-01'),
    strategies: ['momentum', 'mean_reversion'],
    // ... rest of config
  };
  
  return orchestrator.runBacktest(backtestConfig);
}

// Example 3: Modified schedule function to only process symbol X
async function scheduleOperationsOnlyX(handler: any) {
  // Get all symbols that need updates, then filter for X
  const staleSymbols = await handler.operationRegistry.getStaleSymbols('qm', 'price_update', {
    minHoursSinceRun: 24,
    limit: 1000
  });
  
  // Filter to only include symbol X
  const symbolXOnly = staleSymbols.filter((s: string) => s.includes('X:'));
  
  if (symbolXOnly.length > 0) {
    const symbolData = await handler.mongodb.find('qmSymbols', {
      qmSearchCode: symbolXOnly[0]
    });
    
    if (symbolData.length > 0) {
      await handler.scheduleOperation('update-prices', {
        symbol: symbolData[0].symbol,
        symbolId: symbolData[0].symbolId,
        qmSearchCode: symbolData[0].qmSearchCode
      });
    }
  }
}

// Example 4: Modified intraday crawl for symbol X only
async function crawlIntradayOnlyX(handler: any) {
  // Direct approach - just process symbol X
  const symbolX = await handler.mongodb.findOne('qmSymbols', { symbol: 'X' });
  
  if (symbolX) {
    return handler.crawlIntradayData({
      symbol: symbolX.symbol,
      symbolId: symbolX.symbolId,
      qmSearchCode: symbolX.qmSearchCode,
      mode: 'full',
      targetOldestDate: new Date('2020-01-01')
    });
  }
}

// Example 5: Test wrapper that ensures only symbol X is processed
function createSymbolXTestWrapper(originalFunction: Function) {
  return async function(...args: any[]) {
    // Check if first argument has symbol property
    if (args[0] && typeof args[0] === 'object') {
      // If it's a symbol-specific call, only proceed if symbol is X
      if (args[0].symbol && args[0].symbol !== 'X') {
        console.log(`Skipping symbol ${args[0].symbol} - only testing X`);
        return { success: false, message: 'Test mode - only symbol X allowed' };
      }
      
      // If it's a batch operation, filter to only X
      if (args[0].symbols && Array.isArray(args[0].symbols)) {
        args[0].symbols = args[0].symbols.filter((s: string) => s === 'X');
      }
    }
    
    // Call original function with potentially modified args
    return originalFunction.apply(this, args);
  };
}

// Example usage
async function demonstrateUsage() {
  console.log('=== Demonstration of Symbol X Only Modifications ===\n');

  // Mock tracker for demonstration
  const mockTracker = {
    getStaleSymbols: async (provider: string, operation: string, options: any) => {
      console.log(`Getting stale symbols with options:`, options);
      if (options.symbolFilter?.symbol === 'X') {
        return ['X:NYSE'];
      }
      return ['AAPL:NASDAQ', 'GOOGL:NASDAQ', 'X:NYSE', 'MSFT:NASDAQ'];
    }
  } as any;

  // Test the modified function
  console.log('1. Testing getStaleSymbols with X filter:');
  const xOnlySymbols = await getStaleSymbolsOnlyX(mockTracker, 'qm', 'price_update', {
    minHoursSinceRun: 24
  });
  console.log('Results:', xOnlySymbols);

  console.log('\n2. Example of wrapper usage:');
  const originalUpdate = async (input: any) => {
    console.log(`Processing symbol: ${input.symbol}`);
    return { success: true, symbol: input.symbol };
  };
  
  const wrappedUpdate = createSymbolXTestWrapper(originalUpdate);
  
  await wrappedUpdate({ symbol: 'X', symbolId: 123 });
  await wrappedUpdate({ symbol: 'AAPL', symbolId: 456 }); // Will be skipped
  
  console.log('\n=== Demonstration Complete ===');
}

// Run demonstration
demonstrateUsage().catch(console.error);