interface RateLimitConfig {
  maxRequests: number;
  windowMs: number;
}

class RateLimiter {
  private requests: Map<string, number[]> = new Map();

  constructor(private config: RateLimitConfig) {}

  canMakeRequest(userId: string): boolean {
    const now = Date.now();
    const userRequests = this.requests.get(userId) || [];

    const validRequests = userRequests.filter(
      timestamp => now - timestamp < this.config.windowMs
    );

    if (validRequests.length >= this.config.maxRequests) {
      return false;
    }

    validRequests.push(now);
    this.requests.set(userId, validRequests);
    return true;
  }
}

const maxRequests = parseInt(import.meta.env.VITE_OPENAI_RATE_LIMIT_MAX || '10');
const windowMs = parseInt(import.meta.env.VITE_OPENAI_RATE_LIMIT_WINDOW || '3600000');

export const openaiRateLimiter = new RateLimiter({
  maxRequests,
  windowMs
});
