// 단순 정적 파일 서버 (검증용)
const http = require('http');
const fs = require('fs');
const path = require('path');

const PORT = process.env.PORT || 4500;
const ROOT = path.resolve(__dirname, '../public');

const MIME = {
  '.html': 'text/html; charset=utf-8',
  '.css': 'text/css; charset=utf-8',
  '.js': 'application/javascript; charset=utf-8',
  '.json': 'application/json; charset=utf-8',
  '.svg': 'image/svg+xml',
  '.png': 'image/png',
  '.jpg': 'image/jpeg',
  '.ico': 'image/x-icon'
};

http.createServer((req, res) => {
  let url = req.url.split('?')[0];
  if (url.endsWith('/')) url += 'index.html';
  const filePath = path.join(ROOT, decodeURIComponent(url));

  if (!filePath.startsWith(ROOT)) {
    res.writeHead(403); res.end('Forbidden'); return;
  }

  fs.readFile(filePath, (err, data) => {
    if (err) {
      // SPA 폴백 없이 404
      res.writeHead(404, { 'Content-Type': 'text/plain' });
      res.end('Not Found: ' + url);
      return;
    }
    const ext = path.extname(filePath).toLowerCase();
    res.writeHead(200, { 'Content-Type': MIME[ext] || 'application/octet-stream' });
    res.end(data);
  });
}).listen(PORT, () => {
  console.log(`이선생 TKTX local: http://localhost:${PORT}`);
});
