// Passenger-compatible minimal test
// This tests Express WITHOUT canvas/sharp dependencies

const express = require('express');
const app = express();

// Simple routes - no heavy dependencies
app.get('/', (req, res) => {
  res.send(`
    <!DOCTYPE html>
    <html>
    <head>
      <title>CompareKit Passenger Test</title>
      <style>
        body { font-family: Arial; padding: 40px; background: #f0f0f0; }
        .success { color: green; }
        .info { background: white; padding: 20px; border-radius: 5px; margin: 20px 0; }
      </style>
    </head>
    <body>
      <h1 class="success">✅ SUCCESS! Passenger + Express Working!</h1>
      <div class="info">
        <p><strong>Node.js version:</strong> ${process.version}</p>
        <p><strong>Process ID:</strong> ${process.pid}</p>
        <p><strong>Directory:</strong> ${process.cwd()}</p>
        <p><strong>Environment:</strong> ${process.env.NODE_ENV || 'not set'}</p>
      </div>
      <h3>Next Steps:</h3>
      <ul>
        <li><a href="/test-basic">Test Basic Modules (express, cors, dotenv)</a></li>
        <li><a href="/test-heavy">Test Heavy Modules (canvas, sharp)</a></li>
      </ul>
    </body>
    </html>
  `);
});

app.get('/test-basic', (req, res) => {
  const results = {};

  try {
    require('express');
    results.express = '✅ OK';
  } catch (e) {
    results.express = '❌ FAILED: ' + e.message;
  }

  try {
    require('cors');
    results.cors = '✅ OK';
  } catch (e) {
    results.cors = '❌ FAILED: ' + e.message;
  }

  try {
    require('dotenv');
    results.dotenv = '✅ OK';
  } catch (e) {
    results.dotenv = '❌ FAILED: ' + e.message;
  }

  res.json({
    status: 'Basic Module Test',
    results: results
  });
});

app.get('/test-heavy', (req, res) => {
  const results = {};

  try {
    require('canvas');
    results.canvas = '✅ OK - This is surprising! Canvas loaded successfully.';
  } catch (e) {
    results.canvas = '❌ FAILED: ' + e.message.substring(0, 200);
  }

  try {
    require('sharp');
    results.sharp = '✅ OK - Sharp loaded successfully.';
  } catch (e) {
    results.sharp = '❌ FAILED: ' + e.message.substring(0, 200);
  }

  res.json({
    status: 'Heavy Module Test',
    results: results,
    note: 'If these fail, you cannot use canvas/sharp on this hosting'
  });
});

// Health check
app.get('/health', (req, res) => {
  res.json({ status: 'ok', message: 'Passenger test app running' });
});

// Export for Passenger (Passenger handles the port automatically)
module.exports = app;
