<?php

namespace App\Providers;

use App\Models\User;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;
use App\Models\EdarateOstani;

class AuthServiceProvider extends ServiceProvider
{
    /**
     * The policy mappings for the application.
     *
     * @var array
     */
    protected $policies = [
        // 'App\Model' => 'App\Policies\ModelPolicy',
    ];

    /**
     * Register any authentication / authorization services.
     *
     * @return void
     */
    public function boot()
    {
        $this->registerPolicies();

        // Gate::before(function ($user, $ability) {
        //     return $user->hasRole('superadmin') ? true : null;
        // });

        // road item
        Gate::define('gate-supervise-road-item', function ($user, $road_item) {
            return $user->can('supervise-road-item') || 
                    ($user->can('supervise-road-item-province') && $road_item->province_id == $user->province_id) ||
                    ($user->can('supervise-road-item-by-edareostani') && in_array($road_item->item, explode(",", EdarateOstani::where('id', $user->edarate_ostani_id)->first()->items_for_confirm)))
                    ;
        });


        // road patrol
        Gate::define('gate-supervise-road-patrol', function ($user, $road_patrol) {
            return $user->can('supervise-road-patrol') || 
                    ($user->can('supervise-road-patrol-province') && $road_patrol->province_id == $user->province_id);
        });

        // road observed
        Gate::define('gate-supervise-road-observed', function ($user, $road_observed) {
            return $user->can('supervise-fast-react') || 
                    ($user->can('supervise-fast-react-province') && $road_observed->province_id == $user->province_id);
        });

        Gate::define('viewPulse', function (User $user) {
            return $user->username == 'witel';
        });
    }
}
