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

declare global {
  // eslint-disable-next-line @typescript-eslint/no-namespace
  namespace Express {
    interface Locals {
      correlationId: string;
    }
  }
}

function extractCorrelationId(req: Request, res: Response, next: NextFunction) {
  res.locals.correlationId = req.header('X-Unito-Correlation-Id') ?? crypto.randomUUID();

  next();
}

export default extractCorrelationId;
