import { Request, Response, NextFunction } from 'express';

/**
 * UltraMsg clients send `token` in the form body instead of `X-API-Key`. Copy it onto the
 * header so the existing ApiKeyGuard can authenticate — do not call validateApiKey here.
 */
export function ultramsgTokenMiddleware(req: Request, _res: Response, next: NextFunction): void {
  if (!req.headers['x-api-key']) {
    const token = (req.body as { token?: unknown } | undefined)?.token;
    if (typeof token === 'string' && token.length > 0) {
      req.headers['x-api-key'] = token;
    }
  }
  next();
}
