<?php

namespace App\Providers;

use App\Domain\Identity\Services\PermissionService;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     */
    public function register(): void
    {
        //
    }

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        DB::enableQueryLog();

        Gate::before(function ($user, $ability) {
            return app(PermissionService::class)->can($user, $ability) ?: null;
        });

        Blade::if('permission', function (string $permission) {
            $user = auth()->user();
            if (!$user) {
                return false;
            }
            return app(PermissionService::class)->can($user, $permission);
        });

        $this->configureRateLimiting();
    }

    private function configureRateLimiting(): void
    {
        RateLimiter::for('api', function (Request $request) {
            return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
        });

        RateLimiter::for('api-auth', function (Request $request) {
            return Limit::perMinute(10)->by($request->ip());
        });

        RateLimiter::for('api-otp', function (Request $request) {
            return Limit::perMinute(3)->by($request->input('phone', $request->ip()));
        });

        RateLimiter::for('api-payments', function (Request $request) {
            return Limit::perMinute(5)->by($request->user()?->id ?: $request->ip());
        });
    }
}
