#!/usr/bin/env bun

/**
 * Script to check intraday crawl status
 */

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

async function checkIntradayStatus() {
  const context = await createTestContext();
  const handler = new QMHandler(context.services);

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

  try {
    const tracker = handler.operationRegistry.getTracker('qm');
    
    // Get operation stats
    const stats = await tracker.getOperationStats('intraday_bars');
    console.log('Intraday Bars Operation Stats:');
    console.log('------------------------------');
    console.log(`Total symbols: ${stats.totalSymbols}`);
    console.log(`Processed symbols: ${stats.processedSymbols}`);
    console.log(`Successful symbols: ${stats.successfulSymbols}`);
    console.log(`Failed symbols: ${stats.failedSymbols}`);
    console.log(`Stale symbols: ${stats.staleSymbols}`);
    console.log(`Finished crawls: ${stats.finishedCrawls || 0}`);
    console.log('');

    // Get symbols needing crawl
    console.log('Symbols needing crawl (top 10):');
    console.log('--------------------------------');
    const needsCrawl = await tracker.getSymbolsForIntradayCrawl('intraday_bars', {
      limit: 10,
      targetOldestDate: new Date('2020-01-01')
    });

    for (const symbol of needsCrawl) {
      const crawlState = symbol.operationStatus?.crawlState;
      console.log(`\n${symbol.symbol}:`);
      console.log(`  Status: ${symbol.operationStatus?.status || 'never run'}`);
      console.log(`  Last run: ${symbol.operationStatus?.lastRunAt || 'never'}`);
      
      if (crawlState) {
        console.log(`  Finished: ${crawlState.finished}`);
        console.log(`  Oldest date: ${crawlState.oldestDateReached || 'none'}`);
        console.log(`  Newest date: ${crawlState.newestDateReached || 'none'}`);
        console.log(`  Days processed: ${crawlState.totalDaysProcessed || 0}`);
      }
      
      if (symbol.gaps) {
        console.log(`  Gaps: ${symbol.gaps.forward ? 'new data' : ''} ${symbol.gaps.backward ? 'historical' : ''}`);
      }
    }

  } catch (error) {
    console.error('Failed to check status:', error);
  } finally {
    await context.cleanup();
  }
}

// Run the check
checkIntradayStatus().catch(console.error);