#!/usr/bin/env node

/**
 * Docker Build Performance Test Script
 * Tests the optimized Dockerfile with BuildKit and full CPU usage
 */

const { execSync } = require('child_process');
const fs = require('fs');

console.log('🚀 Docker Build Performance Test Suite');
console.log('=====================================\n');

// Test configurations
const tests = [
  {
    name: '🔧 Standard Build',
    command: 'docker compose build',
    description: 'Standard Docker build without optimizations'
  },
  {
    name: '⚡ Optimized Build with BuildKit',
    command: 'DOCKER_BUILDKIT=1 docker compose --progress=plain build',
    description: 'Optimized build with BuildKit and progress output'
  },
  {
    name: '🚀 Parallel Optimized Build',
    command: 'DOCKER_BUILDKIT=1 COMPOSE_BAKE=true docker compose --progress=plain build --parallel',
    description: 'Full CPU parallel build with Bake integration'
  },
  {
    name: '🔄 Development Target Build',
    command: 'DOCKER_BUILDKIT=1 docker compose --progress=plain build --target development',
    description: 'Fast development build for iteration'
  }
];

async function runBuildTest(test) {
  console.log(`\n${test.name}`);
  console.log('─'.repeat(50));
  console.log(`📝 ${test.description}`);
  console.log(`💻 Command: ${test.command}\n`);
  
  const startTime = Date.now();
  
  try {
    console.log('⏱️  Starting build...');
    const output = execSync(test.command, { 
      encoding: 'utf8', 
      maxBuffer: 1024 * 1024 * 10, // 10MB buffer
      timeout: 600000 // 10 minute timeout
    });
    
    const endTime = Date.now();
    const duration = (endTime - startTime) / 1000;
    
    console.log(`✅ Build completed in ${duration.toFixed(2)} seconds`);
    
    // Extract key metrics from build output
    const lines = output.split('\n');
    const cacheHits = lines.filter(line => line.includes('CACHED')).length;
    const layers = lines.filter(line => line.match(/^#\d+/)).length;
    
    console.log(`📊 Metrics:`);
    console.log(`   • Build time: ${duration.toFixed(2)}s`);
    console.log(`   • Cache hits: ${cacheHits}`);
    console.log(`   • Total layers: ${layers}`);
    
    return {
      name: test.name,
      duration,
      cacheHits,
      layers,
      success: true
    };
    
  } catch (error) {
    const endTime = Date.now();
    const duration = (endTime - startTime) / 1000;
    
    console.log(`❌ Build failed after ${duration.toFixed(2)} seconds`);
    console.log(`Error: ${error.message.slice(0, 200)}...`);
    
    return {
      name: test.name,
      duration,
      success: false,
      error: error.message
    };
  }
}

async function testScreenshotService() {
  console.log('\n🖼️  Testing Enhanced Screenshot Service');
  console.log('─'.repeat(50));
  
  try {
    // Start container if not running
    console.log('🔄 Ensuring container is running...');
    execSync('docker compose up -d', { encoding: 'utf8' });
    
    // Wait for service to be ready
    console.log('⏳ Waiting for service to be ready...');
    await new Promise(resolve => setTimeout(resolve, 10000));
    
    // Test the API endpoint
    console.log('🧪 Testing enhanced screenshot API...');
    const testCommand = `curl -X POST http://localhost:3000/api/enhanced-screenshot \\
      -H "Content-Type: application/json" \\
      -d '{"symbol": "SOLUSD", "timeframe": "240", "layouts": ["ai"]}' \\
      --max-time 60 --silent`;
    
    const response = execSync(testCommand, { encoding: 'utf8' });
    console.log('📸 Screenshot API Response:', response.slice(0, 200));
    
    return { success: true, response };
    
  } catch (error) {
    console.log(`❌ Screenshot service test failed: ${error.message}`);
    return { success: false, error: error.message };
  }
}

async function runPerformanceTests() {
  console.log('🎯 Starting Docker optimization performance tests...\n');
  
  const results = [];
  
  // Run build tests
  for (const test of tests) {
    const result = await runBuildTest(test);
    results.push(result);
    
    // Small delay between tests
    await new Promise(resolve => setTimeout(resolve, 2000));
  }
  
  // Test screenshot service
  const screenshotResult = await testScreenshotService();
  results.push({ name: '🖼️ Screenshot Service', ...screenshotResult });
  
  // Generate summary report
  console.log('\n📊 Performance Test Results Summary');
  console.log('═'.repeat(60));
  
  const successfulBuilds = results.filter(r => r.success && r.duration);
  
  if (successfulBuilds.length > 0) {
    const avgBuildTime = successfulBuilds.reduce((sum, r) => sum + r.duration, 0) / successfulBuilds.length;
    const fastestBuild = successfulBuilds.reduce((min, r) => r.duration < min.duration ? r : min);
    
    console.log(`\n🏆 Best Performance: ${fastestBuild.name}`);
    console.log(`   ⏱️  Time: ${fastestBuild.duration.toFixed(2)}s`);
    console.log(`   💾 Cache hits: ${fastestBuild.cacheHits || 0}`);
    
    console.log(`\n📈 Average build time: ${avgBuildTime.toFixed(2)}s`);
  }
  
  console.log('\n📋 Individual Results:');
  results.forEach(result => {
    const status = result.success ? '✅' : '❌';
    const time = result.duration ? `${result.duration.toFixed(2)}s` : 'N/A';
    console.log(`   ${status} ${result.name}: ${time}`);
  });
  
  // Recommendations
  console.log('\n💡 Optimization Recommendations:');
  console.log('   • Use DOCKER_BUILDKIT=1 for faster builds');
  console.log('   • Enable --parallel for multi-threaded builds');
  console.log('   • Use --progress=plain for detailed build output');
  console.log('   • Consider COMPOSE_BAKE=true for better performance');
  console.log('   • Use development target for faster iteration');
  
  return results;
}

// Main execution
if (require.main === module) {
  runPerformanceTests()
    .then(results => {
      console.log('\n🎉 Performance testing completed!');
      
      // Save results to file
      const reportFile = 'docker-performance-report.json';
      fs.writeFileSync(reportFile, JSON.stringify({
        timestamp: new Date().toISOString(),
        results,
        system: {
          nodeVersion: process.version,
          platform: process.platform,
          cpus: require('os').cpus().length
        }
      }, null, 2));
      
      console.log(`📄 Detailed report saved to: ${reportFile}`);
      process.exit(0);
    })
    .catch(error => {
      console.error('\n💥 Performance testing failed:', error.message);
      process.exit(1);
    });
}

module.exports = { runPerformanceTests };
