import { SetMetadata } from "@nestjs/common";

export interface RateLimitOptions {
  /**
   * Maximum number of requests allowed within the TTL window
   */
  limit: number;

  /**
   * Time-to-live in seconds for the rate limit window
   */
  ttl: number;

  /**
   * Optional key prefix for Redis storage (defaults to route path)
   */
  keyPrefix?: string;

  /**
   * Whether to skip rate limiting (useful for conditional logic)
   */
  skip?: boolean;
}

export const RATE_LIMIT_KEY = "RATE_LIMIT_OPTIONS";

/**
 * Decorator to apply rate limiting to a controller or route handler.
 * Uses rate-limiter-flexible with Redis backend.
 *
 * @example
 * ```typescript
 * @RateLimit({ limit: 10, ttl: 60 }) // 10 requests per minute
 * @Get('endpoint')
 * async myEndpoint() { ... }
 * ```
 */
export const RateLimit = (options: RateLimitOptions) => SetMetadata(RATE_LIMIT_KEY, options);

/**
 * Skip rate limiting for this route (useful when applied at controller level)
 */
export const SkipRateLimit = () => SetMetadata(RATE_LIMIT_KEY, { skip: true } as RateLimitOptions);
