/**
 * Test script specifically for symbol X operations
 */

import { QMHandler } from '../src/handlers/qm/qm.handler';
import { OperationTracker } from '../src/shared/operation-manager';
import type { DataIngestionServices } from '../src/types';

// Simple test to check operations for symbol X
async function testSymbolXOperations() {
  console.log('=== Testing Operations for Symbol X ===\n');

  // Mock minimal services needed
  const mockServices: Partial<DataIngestionServices> = {
    mongodb: {
      collection: (name: string) => ({
        find: () => ({
          toArray: async () => {
            console.log(`Querying collection: ${name}`);
            if (name === 'qmSymbols') {
              return [{
                symbol: 'X',
                symbolId: 123456,
                qmSearchCode: 'X:NYSE',
                exchange: 'NYSE',
                name: 'United States Steel Corporation'
              }];
            }
            return [];
          }
        }),
        findOne: async (query: any) => {
          console.log(`Finding one in ${name}:`, query);
          return null;
        },
        updateOne: async (filter: any, update: any, options: any) => {
          console.log(`Updating ${name}:`, { filter, update });
          return { modifiedCount: 1 };
        }
      }),
      find: async (collection: string, query: any) => {
        console.log(`Direct find on ${collection}:`, query);
        if (collection === 'qmSymbols' && query.symbol === 'X') {
          return [{
            symbol: 'X',
            symbolId: 123456,
            qmSearchCode: 'X:NYSE'
          }];
        }
        return [];
      }
    } as any,
    logger: {
      info: (msg: string, data?: any) => console.log(`[INFO] ${msg}`, data || ''),
      error: (msg: string, data?: any) => console.error(`[ERROR] ${msg}`, data || ''),
      warn: (msg: string, data?: any) => console.warn(`[WARN] ${msg}`, data || ''),
      debug: (msg: string, data?: any) => console.debug(`[DEBUG] ${msg}`, data || '')
    }
  } as DataIngestionServices;

  // Test 1: Check stale operations for symbol X
  console.log('Test 1: Get stale operations for symbol X');
  console.log('------------------------------------------');
  
  const tracker = new OperationTracker(mockServices as any);
  
  try {
    // Check each operation type
    const operations = [
      'symbol_info',
      'price_update', 
      'intraday_bars',
      'financials_update_quarterly',
      'financials_update_annual',
      'events_update',
      'filings_update',
      'insiders_update',
      'news_update'
    ];

    for (const operation of operations) {
      console.log(`\nChecking ${operation}:`);
      
      const staleSymbols = await tracker.getStaleSymbols('qm', operation, {
        minHoursSinceRun: 0, // Get all symbols regardless of last run
        limit: 10,
        symbolFilter: { symbol: 'X' } // Only get symbol X
      });
      
      console.log(`Found ${staleSymbols.length} stale symbols:`, staleSymbols);
    }
  } catch (error) {
    console.error('Error checking stale symbols:', error);
  }

  // Test 2: Check intraday crawl status for symbol X
  console.log('\n\nTest 2: Check intraday crawl status for symbol X');
  console.log('------------------------------------------------');
  
  try {
    const symbolsForCrawl = await tracker.getSymbolsForIntradayCrawl('intraday_bars', {
      limit: 10,
      symbolFilter: { symbol: 'X' }
    });
    
    console.log(`Found ${symbolsForCrawl.length} symbols for intraday crawl`);
    if (symbolsForCrawl.length > 0) {
      console.log('Symbol details:', JSON.stringify(symbolsForCrawl[0], null, 2));
    }
  } catch (error) {
    console.error('Error checking intraday crawl:', error);
  }

  console.log('\n=== Tests Complete ===');
}

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