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

// Use DATA_DIR so the non-root container user (UID 1000) can write logs.
// Falls back to ../../data/server.log (same directory index.js uses).
const DATA_DIR = process.env.DATA_DIR || path.join(__dirname, '../../data');
if (!fs.existsSync(DATA_DIR)) fs.mkdirSync(DATA_DIR, { recursive: true });

const logFile = fs.createWriteStream(path.join(DATA_DIR, 'server.log'), { flags: 'a' });

function logToFile(message) {
  logFile.write(`[${new Date().toISOString()}] ${message}\n`);
}

module.exports = {
  logToFile
};
