import rateLimit from 'express-rate-limit';
import RedisStore from 'rate-limit-redis';
import { redis } from '../../config/redis';

/** 3 requests per hour for resending verification emails */
export function createVerificationRateLimit() {
  return rateLimit({
    windowMs: 60 * 60 * 1000,
    max: 3,
    standardHeaders: true,
    legacyHeaders: false,
    store: new RedisStore({
      sendCommand: (command: string, ...args: string[]) => redis.call(command, ...args) as Promise<any>,
      prefix: 'rl:verify-resend:',
    }),
    message: {
      error: {
        message: 'Too many verification email requests, please try again later',
        code: 'VERIFICATION_RATE_LIMIT_EXCEEDED',
      },
    },
  });
}

/** 3 requests per hour for password reset emails */
export function createResetRateLimit() {
  return rateLimit({
    windowMs: 60 * 60 * 1000,
    max: 3,
    standardHeaders: true,
    legacyHeaders: false,
    store: new RedisStore({
      sendCommand: (command: string, ...args: string[]) => redis.call(command, ...args) as Promise<any>,
      prefix: 'rl:password-reset:',
    }),
    message: {
      error: {
        message: 'Too many password reset requests, please try again later',
        code: 'RESET_RATE_LIMIT_EXCEEDED',
      },
    },
  });
}
