local RateLimiterService = {}
RateLimiterService.__index = RateLimiterService

function RateLimiterService.new()
	local self = setmetatable({}, RateLimiterService)
	self.counters = {}
	return self
end

function RateLimiterService:Allow(userId, action, maxCalls, windowSeconds)
	local userCounters = self.counters[userId]
	if userCounters == nil then
		userCounters = {}
		self.counters[userId] = userCounters
	end

	local now = os.clock()
	local bucket = userCounters[action]
	if bucket == nil or (now - bucket.windowStart) > windowSeconds then
		userCounters[action] = { count = 1, windowStart = now }
		return true
	end

	if bucket.count >= maxCalls then
		return false
	end

	bucket.count += 1
	return true
end

function RateLimiterService:ClearUser(userId)
	self.counters[userId] = nil
end

return RateLimiterService
