import { RequestHandler } from 'express';
import { Redis } from 'ioredis';
import { ILogger } from '../../application/ports/logger';
import { requestIdMiddleware } from '../../interface/middleware/request-id.middleware';
import { sanitizationMiddleware } from '../../interface/middleware/sanitization.middleware';
import { auditLoggerMiddleware } from '../../interface/middleware/audit-logger.middleware';
import { createStrictRateLimiter } from '../../interface/middleware/security.middleware';

const STRICT_WINDOW_MS = 60 * 1000; // 1 minute
const STRICT_MAX = 20; // 20 requests per minute

export interface Middlewares {
  requestId: RequestHandler;
  sanitization: RequestHandler;
  auditLogger: RequestHandler;
  strictRateLimiter: RequestHandler;
}

export const createMiddlewares = (logger: ILogger, redis?: Redis): Middlewares => {
  return {
    requestId: requestIdMiddleware,
    sanitization: sanitizationMiddleware,
    auditLogger: auditLoggerMiddleware(logger),
    strictRateLimiter: createStrictRateLimiter(STRICT_WINDOW_MS, STRICT_MAX, redis),
  };
};
