import { enhancedScreenshotService } from './lib/enhanced-screenshot'

async function testDualSessionScreenshots() {
  console.log('🚀 Testing Enhanced Screenshot Service with Dual Sessions')
  
  try {
    // Test configuration
    const config = {
      symbol: 'SOLUSD',
      timeframe: '240',
      layouts: ['ai', 'diy'] as ('ai' | 'diy')[],
      credentials: {
        email: process.env.TRADINGVIEW_EMAIL || '',
        password: process.env.TRADINGVIEW_PASSWORD || ''
      }
    }
    
    console.log('📋 Test Configuration:', config)
    
    // Perform the dual-session screenshot capture
    console.log('\n🔄 Starting dual-session capture...')
    const screenshots = await enhancedScreenshotService.captureWithLogin(config)
    
    console.log('\n✅ Capture completed!')
    console.log(`📸 Screenshots captured: ${screenshots.length}`)
    
    screenshots.forEach((screenshot, index) => {
      console.log(`   ${index + 1}. ${screenshot}`)
    })
    
    if (screenshots.length === 2) {
      console.log('\n🎯 SUCCESS: Both AI and DIY layouts captured successfully!')
    } else {
      console.log('\n⚠️ WARNING: Expected 2 screenshots, got', screenshots.length)
    }
    
    // Test cleanup
    console.log('\n🧹 Testing cleanup...')
    await enhancedScreenshotService.cleanup()
    console.log('✅ Cleanup completed')
    
  } catch (error: any) {
    console.error('\n❌ Test failed:', error.message)
    console.error('Stack trace:', error.stack)
    
    // Ensure cleanup even on error
    try {
      await enhancedScreenshotService.cleanup()
    } catch (cleanupError: any) {
      console.error('Cleanup also failed:', cleanupError.message)
    }
    
    process.exit(1)
  }
}

// Run the test
testDualSessionScreenshots()
  .then(() => {
    console.log('\n🎉 All tests completed successfully!')
    process.exit(0)
  })
  .catch((error) => {
    console.error('\n💥 Test suite failed:', error)
    process.exit(1)
  })
