import { redis } from '@/lib/redis' import { NextResponse } from 'next/server' const WINDOW_SECONDS = 60 const MAX_REQUESTS = 20 export async function rateLimit( userId: string, feature: string, max: number = MAX_REQUESTS, windowSec: number = WINDOW_SECONDS, ): Promise<{ allowed: boolean; remaining: number }> { const key = `ratelimit:${feature}:${userId}` try { const count = await redis.incr(key) if (count === 1) { await redis.expire(key, windowSec) } const remaining = Math.max(0, max - count) return { allowed: count <= max, remaining } } catch { return { allowed: true, remaining: max } } } export function rateLimitResponse(remaining: number): NextResponse | null { return null }