#!/usr/bin/env node

/**
 * Simple test for multi-layout functionality using curl commands
 * This script tests the API endpoints to verify multi-layout screenshot capture
 */

const { exec } = require('child_process');
const fs = require('fs');
const path = require('path');

function executeCommand(command) {
  return new Promise((resolve, reject) => {
    exec(command, (error, stdout, stderr) => {
      if (error) {
        reject({ error, stderr });
      } else {
        resolve(stdout);
      }
    });
  });
}

async function testMultiLayoutAPI() {
  console.log('🔬 Multi-Layout API Test');
  console.log('========================\n');

  const baseUrl = 'http://localhost:3000';
  
  try {
    console.log('1️⃣ Testing server connectivity...');
    
    // Test if server is running
    try {
      await executeCommand(`curl -s -o /dev/null -w "%{http_code}" ${baseUrl}`);
      console.log('✅ Server is running');
    } catch (e) {
      console.log('❌ Server is not running. Please start it with:');
      console.log('   npm run dev');
      console.log('   or');
      console.log('   docker-compose up');
      return;
    }
    
    console.log('\n2️⃣ Updating settings for multi-layout...');
    
    const settingsCommand = `curl -s -X POST ${baseUrl}/api/settings \\
      -H "Content-Type: application/json" \\
      -d '{"symbol": "SOLUSD", "timeframe": "240", "layouts": ["ai", "diy"]}'`;
    
    const settingsResponse = await executeCommand(settingsCommand);
    console.log('Settings response:', settingsResponse);
    
    console.log('\n3️⃣ Triggering multi-layout analysis...');
    
    const analysisCommand = `curl -s -X POST ${baseUrl}/api/analyze \\
      -H "Content-Type: application/json" \\
      -d '{"symbol": "SOLUSD", "timeframe": "240", "layouts": ["ai", "diy"], "useExisting": false}'`;
    
    console.log('🚀 Starting analysis (this may take a while)...');
    console.log('💡 Watch the server logs for detailed layout switching information');
    
    const analysisResponse = await executeCommand(analysisCommand);
    console.log('Analysis response:', analysisResponse);
    
    // Parse response and check screenshots
    try {
      const result = JSON.parse(analysisResponse);
      
      if (result.screenshots && result.screenshots.length > 0) {
        console.log('\n📸 Screenshots captured:');
        result.screenshots.forEach((screenshot, index) => {
          console.log(`   ${index + 1}. ${screenshot}`);
        });
        
        // Check for layout-specific screenshots
        const hasDefaultScreenshot = result.screenshots.some(s => s.includes('_default.png'));
        const hasAIScreenshot = result.screenshots.some(s => s.includes('_ai_'));
        const hasDIYScreenshot = result.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 (hasAIScreenshot && hasDIYScreenshot) {
          console.log('\n🎉 SUCCESS: Multi-layout capture working correctly!');
        } else if (hasDIYScreenshot && !hasAIScreenshot) {
          console.log('\n⚠️ ISSUE: Only DIY layout captured, AI layout switching may have failed');
          console.log('💡 Check the troubleshooting guide: MULTI_LAYOUT_TROUBLESHOOTING.md');
        } else {
          console.log('\n⚠️ ISSUE: Multi-layout capture may not be working as expected');
        }
        
        // Check for debug screenshots
        const screenshotsDir = path.join(process.cwd(), 'screenshots');
        if (fs.existsSync(screenshotsDir)) {
          const files = fs.readdirSync(screenshotsDir);
          const debugFiles = files.filter(f => f.includes('debug_') || f.includes('before_') || f.includes('after_'));
          
          if (debugFiles.length > 0) {
            console.log('\n🔍 Debug screenshots available:');
            debugFiles.slice(0, 10).forEach(file => { // Show first 10
              console.log(`   📋 ${file}`);
            });
            
            if (debugFiles.length > 10) {
              console.log(`   ... and ${debugFiles.length - 10} more debug files`);
            }
            
            console.log('\n💡 Review debug screenshots to see layout switching attempts');
          }
        }
        
      } else {
        console.log('\n❌ No screenshots were captured');
        console.log('💡 Check server logs for errors');
      }
      
      if (result.layoutsAnalyzed) {
        console.log(`\n🎛️ Layouts processed: ${result.layoutsAnalyzed.join(', ')}`);
      }
      
    } catch (parseError) {
      console.log('\n⚠️ Could not parse analysis response as JSON');
      console.log('Raw response:', analysisResponse);
    }
    
  } catch (error) {
    console.error('\n❌ Test failed:', error);
    
    if (error.stderr && error.stderr.includes('Connection refused')) {
      console.log('\n💡 Make sure the Next.js server is running:');
      console.log('   npm run dev');
      console.log('   or');
      console.log('   docker-compose up');
    }
  }
}

async function checkScreenshotsDirectory() {
  console.log('\n4️⃣ Checking screenshots directory...');
  
  const screenshotsDir = path.join(process.cwd(), 'screenshots');
  
  if (!fs.existsSync(screenshotsDir)) {
    console.log('⚠️ Screenshots directory does not exist');
    return;
  }
  
  const files = fs.readdirSync(screenshotsDir);
  const recentFiles = files
    .map(file => ({
      name: file,
      path: path.join(screenshotsDir, file),
      stats: fs.statSync(path.join(screenshotsDir, file))
    }))
    .filter(file => file.stats.isFile() && file.name.endsWith('.png'))
    .sort((a, b) => b.stats.mtime - a.stats.mtime)
    .slice(0, 20); // Show last 20 files
  
  if (recentFiles.length === 0) {
    console.log('⚠️ No screenshot files found');
    return;
  }
  
  console.log(`📂 Found ${files.length} files, showing ${recentFiles.length} most recent:`);
  
  recentFiles.forEach((file, index) => {
    const timeAgo = Math.round((Date.now() - file.stats.mtime) / 1000);
    let timeString;
    
    if (timeAgo < 60) {
      timeString = `${timeAgo}s ago`;
    } else if (timeAgo < 3600) {
      timeString = `${Math.round(timeAgo / 60)}m ago`;
    } else {
      timeString = `${Math.round(timeAgo / 3600)}h ago`;
    }
    
    console.log(`   ${index + 1}. ${file.name} (${timeString})`);
  });
}

async function main() {
  await testMultiLayoutAPI();
  await checkScreenshotsDirectory();
  
  console.log('\n🏁 Test completed');
  console.log('\n📖 For troubleshooting layout switching issues, see:');
  console.log('   MULTI_LAYOUT_TROUBLESHOOTING.md');
}

main().catch(error => {
  console.error('Test script failed:', error);
  process.exit(1);
});
