<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;

final class ApiPlatformServiceProvider extends ServiceProvider
{
    public function boot(): void
    {
        RateLimiter::for('api_public', function (Request $request) {
            return Limit::perMinute(60)->by('ip:'.$request->ip());
        });

        RateLimiter::for('api_v1', function (Request $request) {
            $token = $request->attributes->get('api_token');
            $key = is_object($token) && property_exists($token, 'id')
                ? 'token:'.$token->id
                : 'ip:'.$request->ip();

            return Limit::perMinute(600)->by($key);
        });
    }
}

