#!/usr/bin/env node

/**
 * Test script for multi-layout screenshot functionality
 * Tests AI and DIY layout switching and screenshot capture
 */

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

async function testMultiLayoutScreenshots() {
  console.log('🧪 Testing Multi-Layout Screenshot Functionality\n')
  
  try {
    // Test configuration with multiple layouts
    const testConfig = {
      symbol: 'SOLUSD',
      timeframe: '240', // 4-hour chart
      layouts: ['ai', 'diy'] // Test both AI and DIY layouts
    }
    
    console.log('📋 Test Configuration:')
    console.log(`   Symbol: ${testConfig.symbol}`)
    console.log(`   Timeframe: ${testConfig.timeframe} minutes (4h)`)
    console.log(`   Layouts: ${testConfig.layouts.join(', ')}`)
    console.log('')
    
    console.log('🚀 Starting multi-layout screenshot capture...')
    
    // Capture screenshots with multiple layouts
    const screenshots = await enhancedScreenshotService.captureWithLogin(testConfig)
    
    console.log('\n✅ Multi-layout screenshot test completed!')
    console.log(`📸 Total screenshots captured: ${screenshots.length}`)
    
    if (screenshots.length > 0) {
      console.log('\n📂 Screenshots captured:')
      screenshots.forEach((screenshot, index) => {
        console.log(`   ${index + 1}. ${screenshot}`)
      })
      
      // Expected screenshots:
      // 1. Default layout screenshot
      // 2. AI layout screenshot  
      // 3. DIY layout screenshot
      const expectedCount = 1 + testConfig.layouts.length // default + each layout
      
      if (screenshots.length >= expectedCount) {
        console.log(`\n🎉 SUCCESS: Expected ${expectedCount} screenshots, got ${screenshots.length}`)
        
        // Check if screenshots have the correct naming
        const hasDefaultScreenshot = screenshots.some(s => s.includes('_default.png'))
        const hasAIScreenshot = screenshots.some(s => s.includes('_ai_'))
        const hasDIYScreenshot = screenshots.some(s => s.includes('_diy_'))
        
        console.log('\n🔍 Screenshot Analysis:')
        console.log(`   Default layout: ${hasDefaultScreenshot ? '✅' : '❌'}`)
        console.log(`   AI layout: ${hasAIScreenshot ? '✅' : '❌'}`)
        console.log(`   DIY layout: ${hasDIYScreenshot ? '✅' : '❌'}`)
        
        if (hasDefaultScreenshot && hasAIScreenshot && hasDIYScreenshot) {
          console.log('\n🏆 PERFECT: All expected layouts were captured!')
        } else {
          console.log('\n⚠️  Some layouts may not have been captured correctly')
        }
        
      } else {
        console.log(`\n⚠️  Expected ${expectedCount} screenshots, but got ${screenshots.length}`)
        console.log('   This might indicate layout switching issues')
      }
    } else {
      console.log('\n❌ No screenshots were captured')
    }
    
  } catch (error) {
    console.error('\n❌ Multi-layout screenshot test failed:')
    console.error(error.message)
    
    if (error.message.includes('CAPTCHA') || error.message.includes('manual intervention')) {
      console.log('\n💡 CAPTCHA detected - this is expected in Docker environment')
      console.log('   The manual CAPTCHA handling workflow should have been triggered')
      console.log('   Check if the browser switched to non-headless mode for CAPTCHA solving')
    }
    
    if (error.message.includes('layout')) {
      console.log('\n🔧 Layout switching issues detected:')
      console.log('   - Check if TradingView layout selectors are correct')
      console.log('   - Verify AI and DIY modules are available on the chart')
      console.log('   - Layout detection logic may need refinement')
    }
  }
}

async function testLayoutSwitchingOnly() {
  console.log('\n🔄 Testing Layout Switching Logic Only\n')
  
  try {
    // Get the TradingView automation instance
    const { tradingViewAutomation } = require('./lib/tradingview-automation.ts')
    
    console.log('🔧 Initializing browser...')
    await tradingViewAutomation.init()
    
    console.log('🔐 Checking login status...')
    const isLoggedIn = await tradingViewAutomation.isLoggedIn()
    
    if (!isLoggedIn) {
      console.log('⚠️  Not logged in - attempting smart login...')
      const loginSuccess = await tradingViewAutomation.smartLogin()
      
      if (!loginSuccess) {
        throw new Error('Login required for layout switching test')
      }
    }
    
    console.log('🗺️  Navigating to chart...')
    await tradingViewAutomation.navigateToChart({
      symbol: 'SOLUSD',
      timeframe: '240',
      waitForChart: true
    })
    
    console.log('⏳ Waiting for chart data...')
    await tradingViewAutomation.waitForChartData()
    
    // Test layout switching
    const layoutsToTest = ['ai', 'diy']
    
    for (const layout of layoutsToTest) {
      console.log(`\n🎛️  Testing ${layout.toUpperCase()} layout switch...`)
      
      const switchSuccess = await tradingViewAutomation.switchLayout(layout)
      console.log(`   Switch attempt: ${switchSuccess ? '✅' : '❌'}`)
      
      if (switchSuccess) {
        console.log(`   ⏳ Waiting for ${layout} layout to load...`)
        const loadSuccess = await tradingViewAutomation.waitForLayoutLoad(layout)
        console.log(`   Load detection: ${loadSuccess ? '✅' : '❌'}`)
        
        // Take a test screenshot
        const testScreenshot = `test_${layout}_layout_${Date.now()}.png`
        console.log(`   📸 Taking test screenshot: ${testScreenshot}`)
        await tradingViewAutomation.takeScreenshot(testScreenshot)
      }
    }
    
    console.log('\n✅ Layout switching test completed')
    
  } catch (error) {
    console.error('\n❌ Layout switching test failed:')
    console.error(error.message)
  }
}

async function main() {
  console.log('🔬 Multi-Layout Screenshot Test Suite')
  console.log('=====================================\n')
  
  // Test 1: Full multi-layout screenshot capture
  await testMultiLayoutScreenshots()
  
  // Test 2: Layout switching logic only
  await testLayoutSwitchingOnly()
  
  console.log('\n🏁 Test suite completed')
  console.log('Check the screenshots directory for captured images')
  console.log('Review browser logs for any layout switching issues')
}

// Handle cleanup on exit
process.on('SIGINT', async () => {
  console.log('\n🛑 Test interrupted - cleaning up...')
  try {
    const { tradingViewAutomation } = require('./lib/tradingview-automation.ts')
    await tradingViewAutomation.close()
  } catch (error) {
    // Ignore cleanup errors
  }
  process.exit(0)
})

// Run the tests
main().catch(error => {
  console.error('Test suite failed:', error)
  process.exit(1)
})
