#!/usr/bin/env node

/**
 * Simple health check for enhanced screenshot service in Docker
 */

const http = require('http');

async function testHealthCheck() {
  console.log('🐳 Testing Enhanced Screenshot API health in Docker environment...')
  
  const options = {
    hostname: 'localhost',
    port: 3001,
    path: '/api/enhanced-screenshot',
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
    timeout: 5000
  };

  return new Promise((resolve, reject) => {
    const req = http.request(options, (res) => {
      let data = '';
      
      res.on('data', (chunk) => {
        data += chunk;
      });
      
      res.on('end', () => {
        console.log('✅ API Health Check Response:', data);
        resolve(data);
      });
    });

    req.on('error', (error) => {
      console.error('❌ API Health Check Error:', error.message);
      reject(error);
    });

    req.on('timeout', () => {
      console.error('❌ API Health Check Timeout');
      req.destroy();
      reject(new Error('Request timeout'));
    });

    req.end();
  });
}

async function testSimpleScreenshot() {
  console.log('\n🐳 Testing simple screenshot capture...')
  
  const postData = JSON.stringify({
    symbol: 'SOLUSD',
    timeframe: '240',
    layouts: ['ai'] // Start with just one layout
  });

  const options = {
    hostname: 'localhost',
    port: 3001,
    path: '/api/enhanced-screenshot',
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      'Content-Length': Buffer.byteLength(postData),
    },
    timeout: 120000 // 2 minute timeout for Docker
  };

  return new Promise((resolve, reject) => {
    const req = http.request(options, (res) => {
      let data = '';
      
      res.on('data', (chunk) => {
        data += chunk;
      });
      
      res.on('end', () => {
        console.log('✅ Screenshot API Response:', data);
        resolve(data);
      });
    });

    req.on('error', (error) => {
      console.error('❌ Screenshot API Error:', error.message);
      reject(error);
    });

    req.on('timeout', () => {
      console.error('❌ Screenshot API Timeout');
      req.destroy();
      reject(new Error('Request timeout'));
    });

    req.write(postData);
    req.end();
  });
}

async function main() {
  try {
    // First test health
    await testHealthCheck();
    
    // Then test actual screenshot
    await testSimpleScreenshot();
    
    console.log('\n🎉 All Docker tests completed!');
  } catch (error) {
    console.error('\n💥 Docker test failed:', error.message);
    process.exit(1);
  }
}

main();
