import { appendFileSync } from 'node:fs';

function logStartup(message) {
  const line = `[${new Date().toISOString()}] ${message}\n`;
  try {
    appendFileSync('startup.log', line);
  } catch {
    // Startup log is diagnostic only.
  }
  console.log(line);
}

function logCrash(error) {
  const message = `[${new Date().toISOString()}] ${error?.stack || error?.message || error}\n`;
  try {
    appendFileSync('stderr.log', message);
  } catch {
    // Ignore logging failures so the original error remains the signal.
  }
  console.error(message);
}

logStartup(`Booting app with Node ${process.version}, PORT=${process.env.PORT || 'unset'}, DATA_DIR=${process.env.DATA_DIR || 'unset'}`);

process.on('uncaughtException', (error) => {
  logCrash(error);
  process.exit(1);
});

process.on('unhandledRejection', (error) => {
  logCrash(error);
  process.exit(1);
});

import './server.js';
