// Global Error Handler — hide stack traces in production
function errorHandler(err, req, res, next) {
  console.error('❌ Error:', err.stack || err.message);
  
  const statusCode = err.statusCode || 500;
  const isProd = process.env.NODE_ENV === 'production';
  
  res.status(statusCode).json({
    success: false,
    message: isProd ? 'Terjadi kesalahan internal server' : (err.message || 'Internal Server Error'),
    ...(isProd ? {} : { stack: err.stack })
  });
}

// 404 Not Found Handler
function notFoundHandler(req, res) {
  res.status(404).json({
    success: false,
    message: `Route ${req.method} ${req.originalUrl} tidak ditemukan`
  });
}

module.exports = { errorHandler, notFoundHandler };
