<?php

namespace App\Providers;

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

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

    /**
     * Bootstrap services.
     */
    public function boot(): void
    {
        // Blade directive for role checking
        Blade::if('hasrole', function ($role, $branchCode = null) {
            return auth()->check() && auth()->user()->hasRole($role, $branchCode);
        });

        // Blade directive for permission checking
        Blade::if('hasPermission', function ($permission, $branchCode = null) {
            return auth()->check() && auth()->user()->hasPermission($permission, $branchCode);
        });

        // Blade directive for checking any permission
        Blade::if('hasAnyPermission', function ($permissions, $branchCode = null) {
            if (!auth()->check()) return false;
            
            if (is_string($permissions)) {
                $permissions = explode('|', $permissions);
            }
            
            return auth()->user()->hasAnyPermission($permissions, $branchCode);
        });

        // Blade directive for checking any role
        Blade::if('hasAnyRole', function ($roles, $branchCode = null) {
            if (!auth()->check()) return false;
            
            if (is_string($roles)) {
                $roles = explode('|', $roles);
            }
            
            return auth()->user()->hasAnyRole($roles, $branchCode);
        });

        // Blade directive for admin check
        Blade::if('isAdmin', function () {
            return auth()->check() && auth()->user()->hasRole('admin');
        });
    }
}
