<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use App\Models\User;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class UserRolesController extends Controller
{
    public function index()
    {
        $lists = Role::orderBy('name', 'ASC')->get();
        return view('user.user-roles', compact('lists'));
    }
    /**Function to add a New Role into the Database**/
    public function showAddNewRole()
    {
        return view('user.add-user-role');
    }

    /**A Function to Store the Freshly created Role**/
    public function storeAddNewRole(Request $request)
    {
        $name = $request->input('name');
        $res = 'name exists';
        if(!Role::where('name', $name)->exists()) {
            $role = Role::create(['name' => $name]);
            $res = 'error';
            if($role){
                $res = 'success';
            }
        }
        return $res;
    }

    /**Function to Display Popup window to Affiliate Permissions to a role**/
    public function showAssignPermissionsToRole($id){
        $permissions = Permission::orderBy('name', 'ASC')->get();
        $role = Role::findById($id);
        return view('user.assign-permissions-to-role', compact('permissions', 'id', 'role'));
    }

    /**Store the Selected Permissions into the Database**/
    public function storeAssignPermissionsToRole($id,Request $request){
        $role = Role::findById($id);
        $list = $request['permissions'];
        $res = 'empty list';
        if(!empty($list)){
            foreach ($list as $row){
                $permission = Permission::findById($row);

                $role->givePermissionTo($permission);
            }
            $res = 'success';
        }
        return $res;
    }

    /**Create a Function that will be used to Display all Permissions affiliated to a given Role**/
    public function showViewAssignedPermissionsToRole($id)
    {
        $role = Role::findById($id);
        return view('user.view-assigned-permissions-to-role', compact('role'));
    }

    /**Create a Function that will be used to revoke a particular Permission on a Given Role**/
    public function revokePermission($id,$permission){
        $role = Role::findById($id);
        $res = 'error';
        if( $role->revokePermissionTo($permission)){
            $res = 'success';
        }
        return $res;
    }

    /**Show Pop-Window to Assign Roles to a User**/
    public function showAssignRolesToUser($id){
        $user = User::find($id);
        $roles = Role::orderBy('name', 'ASC')->get();
        return view('user.assign-roles-to-user', compact('roles', 'id', 'user'));
    }

    /**Store the Selected Roles into the User Model**/
    public function storeAssignRolesToUser($id,Request $request){
        $user = User::find($id);
        $list = $request['roles'];
        $res = 'empty list';
        if(!empty($list)){
            foreach ($list as $row){
                $role = Role::findById($row);
                $user->assignRole($role);
            }
            $res = 'success';
        }
        return $res;
    }

    /**Show Pop-Window to Assign Permissions to a User**/
    public function showAssignPermissionsToUser($id){
        $user = User::find($id);
        $permissions = Permission::orderBy('name', 'ASC')->get();
        return view('user.assign-permissions-to-user', compact('permissions', 'id', 'user'));
    }

    /**Store the Selected Roles into the User Model**/
    public function storeAssignPermissionsToUser($id,Request $request){
        $user = User::find($id);
        $list = $request['permissions'];
        $res = 'empty list';
        if(!empty($list)){
            foreach ($list as $row){
                $permission = Permission::findById($row);
                $user->givePermissionTo($permission);
            }
            $res = 'success';
        }
        return $res;
    }


    /**Create a Function that will be used to Display all Roles affiliated to a given User**/
    public function showViewAssignedRolesToAUser($id)
    {
        $user = User::find($id);
        return view('user.view-assigned-roles-to-a-user', compact('user'));
    }

    /**Create a Function that will be used to revoke a particular Role on a Given User**/
    public function revokeUserRole($id,$role){
        $user = User::find($id);
        $role = Role::findById($role);
        $res = 'error';
        if( $user->removeRole($role)){
            $res = 'success';
        }
        return $res;
    }

    /**Create a Function that will be used to Display all Permissions owned by a User Via a Role to a given User**/
    public function showViewAssignedPermissionsViaRolesToAUser($id)
    {
        $user = User::find($id);
        return view('user.view-assigned-permissions-via-roles-to-a-user', compact('user'));
    }


    /**Create a Function that will be used to Display all Direct Permissions affiliated to a given User**/
    public function showViewAssignedDirectPermissionsToAUser($id)
    {
        $user = User::find($id);
        return view('user.view-assigned-direct-permissions-to-a-user', compact('user'));
    }

    /**Create a Function that will be used to revoke a particular Direct Permission on a Given User**/
    public function revokeUserDirectPermission($id,$permission){
        $user = User::find($id);
        $permission = Permission::findById($permission);
        $res = 'error';
        if( $user->revokePermissionTo($permission->name)){
            $res = 'success';
        }
        return $res;
    }

}
