<?php

namespace App\Providers;

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to your application's "home" route.
     *
     * Typically, users are redirected here after authentication.
     *
     * @var string
     */
    public const HOME = '/dashboard';

    /**
     * Define your route model bindings, pattern filters, and other route configuration.
     */
    public function boot(): void
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });

        RateLimiter::for('purchases', function (Request $request) {
            $key = $request->user()?->id ?: $request->ip();

            return [
                Limit::perMinute(10)->by("purchase:burst:{$key}"),
                Limit::perHour(120)->by("purchase:hourly:{$key}"),
            ];
        });

        RateLimiter::for('wallet-transfers', function (Request $request) {
            $key = $request->user()?->id ?: $request->ip();

            return [
                Limit::perMinute(5)->by("wallet-transfer:burst:{$key}"),
                Limit::perHour(60)->by("wallet-transfer:hourly:{$key}"),
            ];
        });

        RateLimiter::for('wallet-beneficiary-lookup', function (Request $request) {
            $key = $request->user()?->id ?: $request->ip();

            return [
                Limit::perMinute(20)->by("wallet-beneficiary:burst:{$key}"),
                Limit::perHour(300)->by("wallet-beneficiary:hourly:{$key}"),
            ];
        });

        RateLimiter::for('wallet-funding-init', function (Request $request) {
            $key = $request->user()?->id ?: $request->ip();

            return [
                Limit::perMinute(4)->by("wallet-funding:burst:{$key}"),
                Limit::perHour(30)->by("wallet-funding:hourly:{$key}"),
            ];
        });

        RateLimiter::for('airtime-to-cash-banks', function (Request $request) {
            $key = $request->user()?->id ?: $request->ip();

            return [
                Limit::perMinute(10)->by("a2c-banks:burst:{$key}"),
                Limit::perHour(120)->by("a2c-banks:hourly:{$key}"),
            ];
        });

        RateLimiter::for('airtime-to-cash-bank-resolve', function (Request $request) {
            $key = $request->user()?->id ?: $request->ip();

            return [
                Limit::perMinute(20)->by("a2c-resolve:burst:{$key}"),
                Limit::perHour(240)->by("a2c-resolve:hourly:{$key}"),
            ];
        });

        RateLimiter::for('reseller-api', function (Request $request) {
            $client = $request->attributes->get('api_client');
            $limit = max(10, (int) ($client?->rate_limit_per_minute ?? 120));
            $key = $client?->id
                ? "reseller-api:client:{$client->id}"
                : "reseller-api:ip:".$request->ip();

            return [
                Limit::perMinute($limit)->by($key),
                Limit::perHour($limit * 20)->by("{$key}:hourly"),
            ];
        });

        $this->routes(function () {
            Route::middleware('api')
                ->prefix('api')
                ->group(base_path('routes/api.php'));

            Route::middleware('web')
                ->group(base_path('routes/web.php'));
        });
    }
}
