<?php
namespace Root\Core\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class RateLimiter
{
    protected int $limit = 60; // تعداد مجاز در دقیقه

    public function handle(Request $request, Closure $next)
    {
        $key = 'rate_limit:' . $request->ip();
        $requests = Cache::get($key, 0);

        if ($requests >= $this->limit) {
            return response()->json(['message' => 'Too Many Requests'], 429);
        }

        Cache::put($key, $requests + 1, now()->addMinute());

        return $next($request);
    }
}
