/**
 * Test script for QM operations
 */

import { QMHandler } from '../src/handlers/qm/qm.handler';
import type { DataIngestionServices } from '../src/types';

// Mock services for testing
const mockServices: Partial<DataIngestionServices> = {
  mongodb: {
    batchUpsert: async (collection: string, data: any[], uniqueKeys: string[]) => {
      console.log(`Mock: Batch upsert to ${collection}`, {
        recordCount: data.length,
        uniqueKeys
      });
      return { insertedCount: data.length, modifiedCount: 0 };
    },
    find: async (collection: string, query: any, options?: any) => {
      console.log(`Mock: Find from ${collection}`, { query, options });
      // Return test symbol for testing
      if (collection === 'qmSymbols' && query.symbol === 'X') {
        return [{
          symbol: 'X',
          symbolId: 123456,
          qmSearchCode: 'X:NYSE',
          exchange: 'NYSE',
          name: 'United States Steel Corporation'
        }];
      }
      return [];
    },
    updateOne: async (collection: string, filter: any, update: any, options?: any) => {
      console.log(`Mock: Update ${collection}`, { filter, update, options });
      return { modifiedCount: 1 };
    }
  },
  cache: {
    get: async (key: string) => {
      console.log(`Mock: Cache get ${key}`);
      return null;
    },
    set: async (key: string, value: any, ttl?: number) => {
      console.log(`Mock: Cache set ${key}`, { ttl });
      return true;
    }
  },
  logger: {
    info: (message: string, data?: any) => {
      console.log(`[INFO] ${message}`, data || '');
    },
    error: (message: string, data?: any) => {
      console.error(`[ERROR] ${message}`, data || '');
    },
    warn: (message: string, data?: any) => {
      console.warn(`[WARN] ${message}`, data || '');
    },
    debug: (message: string, data?: any) => {
      console.debug(`[DEBUG] ${message}`, data || '');
    }
  },
  // Mock operation registry
  operationRegistry: {
    updateOperation: async (provider: string, symbol: string, operation: string, data: any) => {
      console.log(`Mock: Update operation ${provider}/${operation} for ${symbol}`, data);
      return true;
    },
    getStaleSymbols: async (provider: string, operation: string, options: any) => {
      console.log(`Mock: Get stale symbols for ${provider}/${operation}`, options);
      // Return test symbol
      if (options.symbolFilter?.symbol === 'X') {
        return ['X:NYSE'];
      }
      return [];
    }
  }
} as DataIngestionServices;

async function testQMOperations() {
  console.log('=== Testing QM Operations ===\n');

  // Create handler instance
  const handler = new QMHandler(mockServices);
  
  // Wait a bit for initialization
  await new Promise(resolve => setTimeout(resolve, 1000));

  // Test 1: Update Insiders for symbol X
  console.log('Test 1: Update Insiders for symbol X');
  console.log('-------------------------------------');
  try {
    const insidersResult = await handler.updateInsiders({
      symbol: 'X',
      symbolId: 123456,
      qmSearchCode: 'X:NYSE',
      lookbackDays: 30
    });
    console.log('Result:', JSON.stringify(insidersResult, null, 2));
  } catch (error) {
    console.error('Failed:', error);
  }

  console.log('\n');

  // Test 2: Update Symbol News for symbol X
  console.log('Test 2: Update Symbol News for symbol X');
  console.log('----------------------------------------');
  try {
    const newsResult = await handler.updateSymbolNews({
      symbol: 'X',
      symbolId: 123456,
      qmSearchCode: 'X:NYSE',
      lookbackDays: 7
    });
    console.log('Result:', JSON.stringify(newsResult, null, 2));
  } catch (error) {
    console.error('Failed:', error);
  }

  console.log('\n');

  // Test 3: Update General News
  console.log('Test 3: Update General News');
  console.log('---------------------------');
  try {
    const generalNewsResult = await handler.updateGeneralNews({
      categories: ['market', 'economy'],
      lookbackMinutes: 60
    });
    console.log('Result:', JSON.stringify(generalNewsResult, null, 2));
  } catch (error) {
    console.error('Failed:', error);
  }

  console.log('\n');

  // Test 4: Check available operations
  console.log('Test 4: List Available Operations');
  console.log('---------------------------------');
  const operations = [
    'create-session',
    'search-symbols',
    'update-symbol-info',
    'update-financials',
    'update-events',
    'update-filings',
    'update-prices',
    'update-intraday-bars',
    'crawl-intraday-data',
    'update-insiders',
    'update-symbol-news',
    'update-general-news'
  ];
  
  for (const op of operations) {
    const hasOperation = typeof (handler as any)[op.replace(/-/g, '')] === 'function';
    console.log(`${op}: ${hasOperation ? '✓' : '✗'}`);
  }

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

// Run tests
testQMOperations().catch(console.error);