const levels = { debug: 10, info: 20, warn: 30, error: 40 };
const currentLevel = levels[(process.env.LOG_LEVEL || 'info').toLowerCase()] || levels.info;

function log(level, message, meta) {
  if ((levels[level] || 100) < currentLevel) return;
  const ts = new Date().toISOString();
  if (meta !== undefined) {
    console.log(`[${ts}] [${level.toUpperCase()}] ${message}`, meta);
  } else {
    console.log(`[${ts}] [${level.toUpperCase()}] ${message}`);
  }
}

module.exports = {
  debug: (msg, meta) => log('debug', msg, meta),
  info: (msg, meta) => log('info', msg, meta),
  warn: (msg, meta) => log('warn', msg, meta),
  error: (msg, meta) => log('error', msg, meta)
};
