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

export const pollVoteRateLimit = rateLimit({
  windowMs: 60 * 60 * 1000, // 1 hour
  max: 30,
  standardHeaders: true,
  legacyHeaders: false,
  store: new RedisStore({
    sendCommand: (command: string, ...args: string[]) => redis.call(command, ...args) as Promise<any>,
    prefix: 'rl:poll-vote:',
  }),
  message: {
    error: {
      message: 'Too many vote submissions, please try again later',
      code: 'POLL_VOTE_RATE_LIMIT_EXCEEDED',
    },
  },
});

export const pollCommentRateLimit = rateLimit({
  windowMs: 60 * 60 * 1000, // 1 hour
  max: 60,
  standardHeaders: true,
  legacyHeaders: false,
  store: new RedisStore({
    sendCommand: (command: string, ...args: string[]) => redis.call(command, ...args) as Promise<any>,
    prefix: 'rl:poll-comment:',
  }),
  message: {
    error: {
      message: 'Too many comments, please try again later',
      code: 'POLL_COMMENT_RATE_LIMIT_EXCEEDED',
    },
  },
});
