// In-memory rate limiter — suitable for single-container deployments.
// Each bucket tracks hit count within a rolling window.

const buckets = new Map<string, { hits: number; resetAt: number }>();

export function rateLimit(key: string, limit: number, windowMs: number): boolean {
  const now = Date.now();
  const bucket = buckets.get(key);

  if (!bucket || now >= bucket.resetAt) {
    buckets.set(key, { hits: 1, resetAt: now + windowMs });
    return true;
  }

  if (bucket.hits >= limit) return false;
  bucket.hits++;
  return true;
}