import https from 'https';
import http from 'http';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const options = {
  key: fs.readFileSync('./localhost+2-key.pem'),
  cert: fs.readFileSync('./localhost+2.pem')
};

// MIME types mapping
const mimeTypes = {
  '.html': 'text/html; charset=utf-8',
  '.htm': 'text/html; charset=utf-8',
  '.js': 'application/javascript; charset=utf-8',
  '.mjs': 'application/javascript; charset=utf-8',
  '.css': 'text/css; charset=utf-8',
  '.json': 'application/json; charset=utf-8',
  '.png': 'image/png',
  '.jpg': 'image/jpeg',
  '.jpeg': 'image/jpeg',
  '.gif': 'image/gif',
  '.svg': 'image/svg+xml',
  '.ico': 'image/x-icon',
  '.woff': 'font/woff',
  '.woff2': 'font/woff2',
  '.ttf': 'font/ttf',
  '.eot': 'application/vnd.ms-fontobject'
};

const API_TARGET = process.env.API_TARGET || 'http://127.0.0.1:8081';

const server = https.createServer(options, (req, res) => {
  console.log(`📥 ${req.method} ${req.url}`);
  
  // Reverse proxy for API
  if (req.url && req.url.startsWith('/api')) {
    const targetUrl = new URL(API_TARGET);
    const proxiedPath = req.url.replace(/^\/api/, '') || '/';
    // Avoid forwarding content-length; let Node set it
    const headers = { ...req.headers };
    delete headers['content-length'];
    delete headers['host'];
    const reqOpts = {
      hostname: targetUrl.hostname,
      port: targetUrl.port || 80,
      path: proxiedPath,
      method: req.method,
      headers,
    };
    const proxy = http.request(reqOpts, (pres) => {
      res.writeHead(pres.statusCode || 500, pres.headers);
      pres.pipe(res);
    });
    proxy.on('error', (err) => {
      console.error('Proxy error:', err);
      res.writeHead(502);
      res.end('Bad Gateway');
    });
    req.pipe(proxy);
    return;
  }

  let filePath = path.join(__dirname, 'dist', req.url === '/' ? 'index.html' : req.url);
  
  // Security check - prevent directory traversal
  if (!filePath.startsWith(path.join(__dirname, 'dist'))) {
    res.writeHead(403);
    res.end('Forbidden');
    return;
  }

  // Check if file exists
  fs.stat(filePath, (err, stats) => {
    if (err || !stats.isFile()) {
      // File not found, serve index.html for SPA routing
      const indexPath = path.join(__dirname, 'dist', 'index.html');
      fs.readFile(indexPath, (err, data) => {
        if (err) {
          res.writeHead(404);
          res.end('File not found');
          return;
        }
        res.writeHead(200, { 
          'Content-Type': 'text/html; charset=utf-8',
          'Cache-Control': 'no-cache'
        });
        res.end(data);
      });
      return;
    }

    // Read and serve the file
    fs.readFile(filePath, (err, data) => {
      if (err) {
        console.error('Error reading file:', err);
        res.writeHead(500);
        res.end('Server Error');
        return;
      }

      const ext = path.extname(filePath);
      const contentType = mimeTypes[ext] || 'application/octet-stream';
      
      // Set cache headers for static assets
      const cacheControl = ext.match(/\.(css|js|png|jpg|jpeg|gif|svg|ico|woff|woff2|ttf)$/) 
        ? 'public, max-age=31536000' 
        : 'no-cache';
      
      res.writeHead(200, { 
        'Content-Type': contentType,
        'Cache-Control': cacheControl
      });
      res.end(data);
    });
  });
});

server.listen(3443, '0.0.0.0', () => {
  console.log('🚀 Production HTTPS Server running on https://159.195.9.107:3443');
  console.log('📁 Serving files from: ./dist/');
  console.log('🔒 SSL Certificate: localhost+2.pem');
});
