<?php

namespace App\Policies;

use App\MultiProject;
use App\Permission;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Support\Facades\Auth;

class PermissionPolicy
{
    use HandlesAuthorization;

    /**
     * Determine whether the user can view any models.
     *
     * @param  \App\User  $user
     * @return mixed
     */

     public function check(User $user,$permission)
     {
        //  dd($permission);
       
        // $permissionName = json_decode(str_replace("'", '"', $permission));
        try{
        $permissionName=[];
        if(is_array($permission)){
            // $permissionName = json_decode(str_replace("'", '"', $permission));
            $permissionName =  $permission;
           }
           else
           {
            $permissionName[] =  $permission;
           }
       
        //    dd( $permissionName);

        $projectId = request()->cookie('current_project_id');
      
        // $user = Auth::user();
        if (empty($projectId)) {
           
            return false;
            $mproject=  MultiProject::where('name','like','Super Admin')->first();
    
            if(empty($mproject)){
                return false;
            }
            $projectId=  $mproject->id;
            $roles = $user
            ->roles()
             ->where('name', 'like','admin')
            ->whereHas('projects', function ($query) use ($projectId) {
                $query->where('multi_project_id', $projectId);
            })
            ->first();
            if(!empty( $roles)){
                if ($user && $projectId) {
                    $roles = $user->roles()->whereHas('projects', function ($query) use ($projectId) {
                        $query->where('multi_project_id', $projectId);
                    })->get();
                    foreach ($roles as $role) {
                        $permissions = $role->permissions()->whereIn('name', $permissionName)->get();
                        if ($permissions->count() > 0) {
                            return true;
                        }
                    }
                }
            }
            else{
                return false;
            }

          
        }
        
        $projectId= decrypt( $projectId);
       
        if ($user && $projectId) {
            $roles = $user->roles()->whereHas('projects', function ($query) use ($projectId) {
                $query->where('multi_project_id', $projectId);
            })->get();
            foreach ($roles as $role) {
                $permissions = $role->permissions()->whereIn('name', $permissionName)->get();
               
                if ($permissions->count() > 0) {
                    return true;
                }
                
           
            }
             // Clear the cached permissions
            
        }
        return false;
    }
    catch(\Exception $e){
        return false;
    }
      
        //  return $user->hasPermissionTo($permission);
     }
     

    public function viewAny(User $user)
    {
        //
    }

    /**
     * Determine whether the user can view the model.
     *
     * @param  \App\User  $user
     * @param  \App\Permission  $permission
     * @return mixed
     */
    public function view(User $user, Permission $permission)
    {
        //
    }

    /**
     * Determine whether the user can create models.
     *
     * @param  \App\User  $user
     * @return mixed
     */
    public function create(User $user)
    {
        //
    }

    /**
     * Determine whether the user can update the model.
     *
     * @param  \App\User  $user
     * @param  \App\Permission  $permission
     * @return mixed
     */
    public function update(User $user, Permission $permission)
    {
        //
    }

    /**
     * Determine whether the user can delete the model.
     *
     * @param  \App\User  $user
     * @param  \App\Permission  $permission
     * @return mixed
     */
    public function delete(User $user, Permission $permission)
    {
        //
    }

    /**
     * Determine whether the user can restore the model.
     *
     * @param  \App\User  $user
     * @param  \App\Permission  $permission
     * @return mixed
     */
    public function restore(User $user, Permission $permission)
    {
        //
    }

    /**
     * Determine whether the user can permanently delete the model.
     *
     * @param  \App\User  $user
     * @param  \App\Permission  $permission
     * @return mixed
     */
    public function forceDelete(User $user, Permission $permission)
    {
        //
    }
}
