#!/usr/bin/env node

/**
 * Parallel Screenshot Batch Test for Scalping Timeframes
 * Uses the most efficient batch capture system for all relevant timeframes
 */

const { BatchScreenshotService } = require('./lib/enhanced-screenshot-batch.ts')

// Scalping-focused timeframes (from seconds to hours)
const SCALP_TIMEFRAMES = [
  '1m',   // 1 minute - ultra-short scalping
  '3m',   // 3 minutes - short scalping
  '5m',   // 5 minutes - standard scalping
  '15m',  // 15 minutes - swing scalping
  '30m',  // 30 minutes - longer scalping
  '1h',   // 1 hour - context timeframe
  '4h'    // 4 hours - trend context
]

// TradingView timeframe mappings
const TIMEFRAME_MAP = {
  '1m': '1',
  '3m': '3', 
  '5m': '5',
  '15m': '15',
  '30m': '30',
  '1h': '60',
  '4h': '240'
}

async function testScalpBatchScreenshots() {
  console.log('🚀 SCALP PRESET: Parallel Batch Screenshot Test')
  console.log('⚡ Capturing ALL scalping timeframes in parallel sessions')
  
  try {
    // Convert to TradingView format
    const timeframes = SCALP_TIMEFRAMES.map(tf => TIMEFRAME_MAP[tf])
    
    const config = {
      symbol: 'SOLUSD',
      timeframes: timeframes,
      layouts: ['ai', 'diy'], // Both AI and DIY layouts
      credentials: {
        email: process.env.TRADINGVIEW_EMAIL || '',
        password: process.env.TRADINGVIEW_PASSWORD || ''
      }
    }
    
    console.log('📋 Batch Configuration:')
    console.log(`   📊 Symbol: ${config.symbol}`)
    console.log(`   ⏱️ Timeframes: ${SCALP_TIMEFRAMES.join(', ')} (${timeframes.length} total)`)
    console.log(`   🎨 Layouts: ${config.layouts.join(', ')} (${config.layouts.length} total)`)
    console.log(`   📸 Total Screenshots: ${timeframes.length * config.layouts.length}`)
    
    // Initialize batch service
    const batchService = new BatchScreenshotService(`scalp_test_${Date.now()}`)
    
    console.log('\n🔄 Starting parallel batch capture...')
    const startTime = Date.now()
    
    // Execute the batch capture
    const batches = await batchService.captureMultipleTimeframes(config)
    
    const endTime = Date.now()
    const duration = (endTime - startTime) / 1000
    
    // Results analysis
    console.log('\n✅ BATCH CAPTURE COMPLETED!')
    console.log(`⏱️ Total Duration: ${duration.toFixed(2)} seconds`)
    console.log(`📸 Screenshots Captured: ${batches.length}/${timeframes.length * config.layouts.length}`)
    console.log(`🚀 Efficiency: ${(batches.length / duration).toFixed(1)} screenshots/second`)
    
    // Group results by layout and timeframe
    const aiScreenshots = batches.filter(b => b.layout === 'ai')
    const diyScreenshots = batches.filter(b => b.layout === 'diy')
    
    console.log('\n📊 RESULTS BREAKDOWN:')
    console.log(`🤖 AI Layout: ${aiScreenshots.length}/${timeframes.length} screenshots`)
    console.log(`🔧 DIY Layout: ${diyScreenshots.length}/${timeframes.length} screenshots`)
    
    // Display captured files by timeframe
    console.log('\n📁 CAPTURED FILES:')
    SCALP_TIMEFRAMES.forEach((tf, index) => {
      const tvTimeframe = timeframes[index]
      const aiBatch = batches.find(b => b.timeframe === tvTimeframe && b.layout === 'ai')
      const diyBatch = batches.find(b => b.timeframe === tvTimeframe && b.layout === 'diy')
      
      console.log(`   ${tf.padEnd(4)} (${tvTimeframe}):`)
      if (aiBatch) {
        console.log(`      🤖 AI:  ${aiBatch.filepath}`)
      } else {
        console.log(`      🤖 AI:  ❌ Failed`)
      }
      if (diyBatch) {
        console.log(`      🔧 DIY: ${diyBatch.filepath}`)
      } else {
        console.log(`      🔧 DIY: ❌ Failed`)
      }
    })
    
    // Success rate analysis
    const successRate = (batches.length / (timeframes.length * config.layouts.length) * 100).toFixed(1)
    console.log(`\n📈 SUCCESS RATE: ${successRate}%`)
    
    if (batches.length === timeframes.length * config.layouts.length) {
      console.log('🎉 PERFECT SUCCESS: All timeframes captured successfully!')
    } else {
      console.log('⚠️  Some screenshots failed - check logs above')
    }
    
    // Performance benchmark
    const avgTimePerScreenshot = duration / batches.length
    console.log(`\n⚡ PERFORMANCE METRICS:`)
    console.log(`   • Average time per screenshot: ${avgTimePerScreenshot.toFixed(2)}s`)
    console.log(`   • Parallel efficiency gain: ~${Math.round(100 - (duration / (batches.length * 10)) * 100)}%`)
    console.log(`   • Total time saved vs sequential: ~${((batches.length * 10) - duration).toFixed(0)}s`)
    
    // Cleanup
    console.log('\n🧹 Cleaning up browser sessions...')
    await batchService.cleanup()
    console.log('✅ Cleanup completed')
    
  } catch (error) {
    console.error('\n❌ Batch test failed:', error.message)
    console.error('Stack trace:', error.stack)
    process.exit(1)
  }
}

// Run the test
testScalpBatchScreenshots()
