// BARE MINIMUM Passenger test
// Uses ONLY Node.js built-in modules - NO npm packages at all
// If this fails, it's a Passenger configuration issue

const http = require('http');

// Create the simplest possible HTTP handler
function handleRequest(req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/html; charset=UTF-8'
  });

  res.end(`<!DOCTYPE html>
<html>
<head>
  <title>Bare Minimum Test</title>
  <meta charset="UTF-8">
</head>
<body style="font-family: Arial; padding: 40px; background: #f0f0f0;">
  <h1 style="color: green;">✅ BASIC NODE.JS WORKS!</h1>
  <h2>Passenger is running Node.js successfully</h2>
  <div style="background: white; padding: 20px; border-radius: 5px; margin: 20px 0;">
    <p><strong>Node.js version:</strong> ${process.version}</p>
    <p><strong>Process ID:</strong> ${process.pid}</p>
    <p><strong>Working directory:</strong> ${process.cwd()}</p>
    <p><strong>Platform:</strong> ${process.platform}</p>
    <p><strong>Architecture:</strong> ${process.arch}</p>
  </div>
  <hr>
  <p><strong>This test uses ZERO npm packages</strong> - only Node.js built-in HTTP module.</p>
  <p>If you see this page, Passenger and Node.js are working correctly.</p>
  <p>The problem is with your npm packages (likely canvas or sharp).</p>
</body>
</html>`);
}

// Create HTTP server
const app = http.createServer(handleRequest);

// Export for Passenger
// Passenger will call this and manage the port itself
if (typeof PhusionPassenger !== 'undefined') {
  PhusionPassenger.configure({ autoInstall: false });
}

module.exports = app;
