import { Request, Response, NextFunction } from 'express';

/**
 * Minimal structured request logger: method, path, status, duration, tenant.
 * Skips health checks to keep logs readable. For production, forward these
 * lines to your log aggregator; wire Sentry in the error handler.
 */
export function requestLogger(req: Request, res: Response, next: NextFunction): void {
  if (req.path.startsWith('/api/health')) return next();
  const start = Date.now();
  res.on('finish', () => {
    const ms = Date.now() - start;
    const tenant = req.user?.tenantId ?? '-';
    const role = req.user?.role ?? '-';
    // eslint-disable-next-line no-console
    console.log(`${req.method} ${req.originalUrl} ${res.statusCode} ${ms}ms tenant=${tenant} role=${role}`);
  });
  next();
}
