<?php

namespace App\Providers;

use Illuminate\Support\Facades\Blade;
use Illuminate\Support\ServiceProvider;

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

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        // Blade directive for checking permissions
        Blade::directive('can', function ($permission) {
            return "<?php if(auth()->check() && auth()->user()->hasPermissionTo($permission)): ?>";
        });

        Blade::directive('endcan', function () {
            return "<?php endif; ?>";
        });

        // Blade directive for checking roles
        Blade::directive('role', function ($role) {
            return "<?php if(auth()->check() && auth()->user()->hasRole($role)): ?>";
        });

        Blade::directive('endrole', function () {
            return "<?php endif; ?>";
        });

        // Blade directive for checking any role
        Blade::directive('hasanyrole', function ($roles) {
            return "<?php if(auth()->check() && auth()->user()->hasAnyRole($roles)): ?>";
        });

        Blade::directive('endhasanyrole', function () {
            return "<?php endif; ?>";
        });
    }
}
