const { spawn } = require('child_process');
const fs = require('fs');
const path = require('path');

const logFile = path.join(__dirname, 'dev.log');
const logStream = fs.openSync(logFile, 'a');

const child = spawn(
  process.execPath,
  [path.join(__dirname, 'node_modules/.bin/next'), 'dev', '-p', '3000'],
  {
    detached: true,
    stdio: ['ignore', logStream, logStream],
    env: { ...process.env },
    cwd: __dirname
  }
);

child.unref();

// Write PID for monitoring
fs.writeFileSync(path.join(__dirname, '.server-pid'), String(child.pid));
console.log(`Daemon started with PID: ${child.pid}`);