#!/usr/bin/env bun

/**
 * Test script for intraday crawl functionality
 */

import { createTestContext } from '../src/test-utils';
import { QMHandler } from '../src/handlers/qm/qm.handler';

async function testIntradayCrawl() {
  console.log('Testing intraday crawl functionality...\n');

  const context = await createTestContext();
  const handler = new QMHandler(context.services);

  // Wait for operation registry to initialize
  await new Promise(resolve => setTimeout(resolve, 1000));

  try {
    // Test 1: Schedule crawls for never-run symbols
    console.log('Test 1: Scheduling crawls for never-run symbols...');
    const result1 = await handler.scheduleIntradayCrawls({
      limit: 5,
      priorityMode: 'never_run',
      targetOldestDate: '2023-01-01' // Just 1 year for testing
    });
    console.log('Result:', result1);
    console.log('');

    // Test 2: Check crawl state for a specific symbol
    console.log('Test 2: Checking crawl state for symbol X...');
    const tracker = handler.operationRegistry.getTracker('qm');
    const isComplete = await tracker.isIntradayCrawlComplete('X', 'intraday_bars', new Date('2023-01-01'));
    console.log('Is crawl complete for X?', isComplete);
    
    // Get detailed state
    const symbols = await tracker.getSymbolsForIntradayCrawl('intraday_bars', {
      limit: 1,
      targetOldestDate: new Date('2023-01-01')
    });
    
    const symbolX = symbols.find(s => s.symbol === 'X');
    if (symbolX) {
      console.log('Symbol X state:', JSON.stringify(symbolX, null, 2));
    }
    console.log('');

    // Test 3: Manually crawl a single symbol
    console.log('Test 3: Manually crawling intraday data for X...');
    
    // First get symbol data
    const symbolData = await context.services.mongodb.findOne('qmSymbols', {
      symbol: 'X'
    });

    if (symbolData && symbolData.symbolId) {
      const crawlResult = await handler.crawlIntradayData({
        symbol: 'X',
        symbolId: symbolData.symbolId,
        qmSearchCode: symbolData.qmSearchCode,
        targetOldestDate: '2024-01-01', // Just current year for quick test
        batchSize: 7
      });
      console.log('Crawl result:', crawlResult);
    } else {
      console.log('Symbol X not found or missing symbolId');
    }

  } catch (error) {
    console.error('Test failed:', error);
  } finally {
    await context.cleanup();
  }
}

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