<?php

namespace App\Providers;

use App\Domain\Identity\Services\PermissionService;
use Illuminate\Support\Facades\Blade;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Gate;
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);
        });
    }
}
