#!/usr/bin/env node

/**
 * Test script for the new optimized multi-timeframe analysis
 * This demonstrates the speed improvements over traditional sequential processing
 */

console.log('🚀 Testing Optimized Multi-Timeframe Analysis')
console.log('=' .repeat(60))

async function testOptimizedAnalysis() {
  try {
    // Test configuration - multiple timeframes
    const config = {
      symbol: 'SOLUSD',
      timeframes: ['1h', '4h'],  // Start with 2 timeframes
      layouts: ['ai', 'diy'],
      analyze: true
    }
    
    console.log('📋 Test Configuration:')
    console.log(`   Symbol: ${config.symbol}`)
    console.log(`   Timeframes: ${config.timeframes.join(', ')}`)
    console.log(`   Layouts: ${config.layouts.join(', ')}`) 
    console.log(`   Expected Screenshots: ${config.timeframes.length * config.layouts.length}`)
    console.log(`   Traditional Time Estimate: ~${config.timeframes.length * 15}s`)
    console.log('')

    // Test API endpoint availability
    console.log('🔍 Checking optimized API endpoint...')
    try {
      const healthResponse = await fetch('http://localhost:3000/api/analysis-optimized')
      if (!healthResponse.ok) {
        throw new Error(`API endpoint not available: ${healthResponse.status}`)
      }
      const healthData = await healthResponse.json()
      console.log('✅ Optimized API endpoint available')
      console.log(`   📄 Description: ${healthData.description}`)
      console.log('')
    } catch (healthError) {
      console.error('❌ API endpoint health check failed:', healthError.message)
      console.log('\n💡 Make sure to start the development server:')
      console.log('   npm run docker:dev')
      console.log('   # OR')
      console.log('   npm run dev')
      process.exit(1)
    }

    // Perform the optimized analysis
    console.log('🔄 Starting optimized multi-timeframe analysis...')
    const startTime = Date.now()
    
    const response = await fetch('http://localhost:3000/api/analysis-optimized', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json'
      },
      body: JSON.stringify(config)
    })
    
    const endTime = Date.now()
    const actualDuration = ((endTime - startTime) / 1000).toFixed(2)
    
    if (!response.ok) {
      const errorData = await response.json()
      throw new Error(`API request failed: ${errorData.error || response.statusText}`)
    }
    
    const result = await response.json()
    
    console.log('\n✅ OPTIMIZED ANALYSIS COMPLETED!')
    console.log('=' .repeat(60))
    
    // Performance metrics
    console.log('📊 PERFORMANCE METRICS:')
    console.log(`   ⏱️ Actual Duration: ${actualDuration}s`)
    console.log(`   ⚡ Reported Duration: ${result.optimization?.totalTime || 'N/A'}`)
    console.log(`   📈 Efficiency Gain: ${result.optimization?.efficiency || 'N/A'}`)
    console.log(`   🖼️ Screenshots Captured: ${result.screenshots?.length || 0}`)
    console.log(`   🤖 AI Calls Made: ${result.optimization?.aiCalls || 0}`)
    console.log('')

    // Screenshot results
    if (result.screenshots?.length > 0) {
      console.log('📸 SCREENSHOT RESULTS:')
      const timeframeGroups = {}
      result.screenshots.forEach((screenshot, index) => {
        const tf = screenshot.timeframe
        if (!timeframeGroups[tf]) timeframeGroups[tf] = []
        timeframeGroups[tf].push(screenshot)
        console.log(`   ${index + 1}. ${screenshot.timeframe} ${screenshot.layout}: ${screenshot.url}`)
      })
      console.log('')
      
      console.log('📊 SCREENSHOT DISTRIBUTION:')
      Object.entries(timeframeGroups).forEach(([timeframe, screenshots]) => {
        console.log(`   ${timeframe}: ${screenshots.length} screenshots`)
      })
      console.log('')
    }

    // AI Analysis results
    if (result.analysis) {
      console.log('🤖 AI ANALYSIS RESULTS:')
      console.log(`   📊 Overall Sentiment: ${result.analysis.marketSentiment}`)
      console.log(`   📈 Recommendation: ${result.analysis.overallRecommendation}`)
      console.log(`   🎯 Confidence: ${result.analysis.confidence}%`)
      console.log('')
      
      // Multi-timeframe breakdown
      if (result.analysis.multiTimeframeAnalysis) {
        console.log('⏰ MULTI-TIMEFRAME BREAKDOWN:')
        Object.entries(result.analysis.multiTimeframeAnalysis).forEach(([timeframe, data]) => {
          console.log(`   ${timeframe}:`)
          console.log(`     📊 Sentiment: ${data.sentiment}`)
          console.log(`     💪 Strength: ${data.strength}%`)
          console.log(`     🎯 Support: $${data.keyLevels?.support?.join(', $') || 'N/A'}`)
          console.log(`     🔴 Resistance: $${data.keyLevels?.resistance?.join(', $') || 'N/A'}`)
        })
        console.log('')
      }
      
      // Consensus
      if (result.analysis.consensus) {
        console.log('🎯 CONSENSUS ANALYSIS:')
        console.log(`   📈 Direction: ${result.analysis.consensus.direction}`)
        console.log(`   🎯 Confidence: ${result.analysis.consensus.confidence}%`)
        console.log(`   💡 Reasoning: ${result.analysis.consensus.reasoning}`)
        if (result.analysis.consensus.conflictingSignals?.length > 0) {
          console.log(`   ⚠️ Conflicts: ${result.analysis.consensus.conflictingSignals.join(', ')}`)
        }
        console.log('')
      }
      
      // Trading setup
      if (result.analysis.tradingSetup) {
        const setup = result.analysis.tradingSetup
        console.log('💰 TRADING SETUP:')
        console.log(`   🎯 Entry: $${setup.entry.price}${setup.entry.buffer ? ' ' + setup.entry.buffer : ''}`)
        console.log(`   🛑 Stop Loss: $${setup.stopLoss.price}`)
        console.log(`   🥉 TP1: $${setup.takeProfits.tp1.price} (${setup.takeProfits.tp1.description})`)
        console.log(`   🥈 TP2: $${setup.takeProfits.tp2.price} (${setup.takeProfits.tp2.description})`)
        console.log(`   ⚖️ Risk/Reward: ${setup.riskToReward}`)
        console.log(`   🎚️ Leverage: ${setup.timeframeRisk.leverageRecommendation}`)
        console.log('')
      }
    } else {
      console.log('⚠️ No AI analysis results received')
      console.log('')
    }

    // Success summary
    console.log('🎉 TEST SUMMARY:')
    console.log(`   ✅ API Response: ${response.ok ? 'Success' : 'Failed'}`)
    console.log(`   ⏱️ Duration: ${actualDuration}s`)
    console.log(`   📸 Screenshots: ${result.screenshots?.length || 0}/${config.timeframes.length * config.layouts.length}`)
    console.log(`   🤖 Analysis: ${result.analysis ? 'Complete' : 'Missing'}`)
    console.log(`   📊 Success Rate: ${((result.screenshots?.length || 0) / (config.timeframes.length * config.layouts.length) * 100).toFixed(0)}%`)
    
    if (result.optimization) {
      console.log(`   🚀 Optimization: ${result.optimization.efficiency}`)
      console.log(`   💰 Cost Savings: ${config.timeframes.length - (result.optimization.aiCalls || 0)} fewer AI calls`)
    }
    
    console.log('\n🎯 The optimized analysis system is working correctly!')
    
  } catch (error) {
    console.error('\n❌ Test failed:', error.message)
    console.error('Stack trace:', error.stack)
    process.exit(1)
  }
}

// Run the test
testOptimizedAnalysis()
