const http = require('http');
const fs = require('fs');
const path = require('path');
const url = require('url');

const PORT = 8080;
const RESUME_DIR = path.join(__dirname, '..', 'docker', 'resume');

// MIME types for common file extensions
const MIME_TYPES = {
  '.html': 'text/html',
  '.css': 'text/css',
  '.js': 'text/javascript',
  '.json': 'application/json',
  '.png': 'image/png',
  '.jpg': 'image/jpeg',
  '.jpeg': 'image/jpeg',
  '.gif': 'image/gif',
  '.svg': 'image/svg+xml',
  '.ico': 'image/x-icon',
  '.txt': 'text/plain',
};

// Create a simple HTTP server
const server = http.createServer((req, res) => {
  // Parse the request URL
  const parsedUrl = url.parse(req.url);
  let pathname = parsedUrl.pathname;
  
  // Set default file to index.html
  if (pathname === '/') {
    pathname = '/index.html';
  }
  
  // Construct the file path
  const filePath = path.join(RESUME_DIR, pathname);
  
  // Get the file extension
  const ext = path.extname(filePath);
  
  // Set the content type based on the file extension
  const contentType = MIME_TYPES[ext] || 'application/octet-stream';
  
  // Read the file and serve it
  fs.readFile(filePath, (err, data) => {
    if (err) {
      // If the file doesn't exist, return 404
      if (err.code === 'ENOENT') {
        res.writeHead(404, { 'Content-Type': 'text/plain' });
        res.end('404 Not Found');
        console.log(`404: ${pathname}`);
        return;
      }
      
      // For other errors, return 500
      res.writeHead(500, { 'Content-Type': 'text/plain' });
      res.end('500 Internal Server Error');
      console.error(`Error serving ${pathname}:`, err);
      return;
    }
    
    // If file is found, serve it with the correct content type
    res.writeHead(200, { 'Content-Type': contentType });
    res.end(data);
    console.log(`200: ${pathname}`);
  });
});

// Start the server
server.listen(PORT, () => {
  console.log(`Server running at http://localhost:${PORT}/`);
  console.log(`Serving files from: ${RESUME_DIR}`);
}); 