<?php

namespace Mehrab\RolePermission\Http\Controllers;

use App\Http\Controllers\Controller;
use Mehrab\RolePermission\Http\Requests\role_store_request;
use Mehrab\RolePermission\Http\Requests\role_update_request;
use Mehrab\RolePermission\Repositories\PermissionRepo;
use Mehrab\RolePermission\Repositories\RoleRepo;
use Spatie\Permission\Models\Permission;
use Mehrab\RolePermission\Models\Role;

class RolePermissionController extends Controller
{
    private $RoleRepo;
    private $PermissionRepo;

    public function __construct(RoleRepo $roleRepo,PermissionRepo $permissionRepo)
    {
        $this->RoleRepo = $roleRepo;
        $this->PermissionRepo = $permissionRepo;
    }

    public function index(PermissionRepo $permissionRepo){
        $this->authorize('manage',Role::class);
        $roles = $this->RoleRepo->all();
        $permissions = $this->PermissionRepo->get_all();
        return view('RolePermission::Admin.list_role_permission',compact('permissions','roles'));

    }

    public function create()
    {
        $this->authorize('create',Role::class);
        $permissions = $this->PermissionRepo->get_all();
        return view('RolePermission::Admin.create_role_permission',compact('permissions'));
    }

    public function store(role_store_request $request){
        $this->authorize('create',Role::class);
         $this->RoleRepo->create($request);
         return redirect()->route('role_permission.index')->with('status','ok');
    }

    public function edit($roleID){
        $this->authorize('update',Role::class);
        $role =  $this->RoleRepo->findByID($roleID);
      $permissions = $this->PermissionRepo->get_all();
      return view('RolePermission::Admin.edit_role_permission',compact('role','permissions'));
    }

    public function update(role_update_request $request,$id){
        $this->authorize('update',Role::class);
        $this->RoleRepo->update($request,$id);
        return redirect()->route('role_permission.index')->with('status','updatedRole');
    }

    public function destroy($id){
        $this->authorize('delete',Role::class);
        return  $this->RoleRepo->delete($id);
    }

}
