/**
 * Request Logging Middleware
 * Logs all incoming HTTP requests with timing information
 */

const logger = require("../utils/logger");

const requestLogger = (req, res, next) => {
  const startTime = Date.now();

  // Log when response is finished
  res.on("finish", () => {
    const duration = Date.now() - startTime;
    
    // Skip logging for health checks in production to reduce noise
    if (process.env.NODE_ENV === 'production' && req.path === '/api/health') {
      return;
    }

    logger.http(req.method, req.path, res.statusCode, duration, {
      ip: req.ip,
      userAgent: req.get('user-agent'),
      userId: req.user?.id,
    });
  });

  next();
};

module.exports = requestLogger;
