<?php

namespace App\Providers;

use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use JeroenNoten\LaravelAdminLte\Events\BuildingMenu;
use Spatie\Permission\Models\Role;

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

    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        //create a gate to check role for menu items
        Gate::define('isAdmin', function ($user) {
            return $user->hasRole(['admin','super admin']);
        });

        Gate::define('isSuperAdmin', function ($user) {
            return $user->hasRole(['super admin']);
        });

        Gate::define('isClient', function ($user) {
            return $user->hasRole(['client']);
        });

        // contracts gates
        Gate::define('isContractAdmin', function ($user) {
            //$role = Role::where("name", "CID-".$user->contract_rid)->get();
            return $user->hasRole('Contract Admin');
        });

        // basic search route
        Gate::define('basicSearch', function ($user) {
            $role = Role::where("name", "CID-".$user->contract_rid)->first();
            if ($role != null) {
                return $role->hasPermissionTo('Basic Search');
            }
            return false;
        });

        // advance search route
        Gate::define('advanceSearch', function ($user) {
            $role = Role::where("name", "CID-".$user->contract_rid)->first();
            if ($role) {
                return $role->hasPermissionTo('Advanced Search');
            }
            return false;
        });
    }
}
