<?php

namespace App\Http\Controllers\Admin\Pages\Roles;

use App\Http\Requests\Roles\RoleStoreRequest;
use App\Http\Requests\Roles\RoleUpdateRequest;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Routing\Controller;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Yajra\DataTables\DataTables;

class RoleController extends Controller
{

    public function __construct()
    {
        $this->middleware('permission:view roles')->only('index', 'ajax');
        $this->middleware('permission:create role')->only('create');
        $this->middleware('permission:store role')->only('store');
        $this->middleware('permission:edit role')->only('edit');
        $this->middleware('permission:update role')->only('update');
        $this->middleware('permission:destroy role')->only('destroy');
    }

    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return view('app.admin.pages.roles.all-roles');
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $permissions = Permission::all();
        return view('app.admin.pages.roles.create-role', compact('permissions'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(RoleStoreRequest $request)
    {
        $role = Role::create(['name' => $request->input('name')]);
        $role->syncPermissions($request->input('permissions') ?? []);
        return back()->with('success', __('Data successfully created!'));
    }

    /**
     * Display the specified resource.
     */
    public function show(Role $role)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(Role $role)
    {
        $permissions = Permission::all();
        return view('app.admin.pages.roles.edit-role', compact([
            'role',
            'permissions'
        ]));
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(RoleUpdateRequest $request, Role $role)
    {
        $role->update(['name' => strtolower($request->input('name'))]);
        $role->syncPermissions($request->permissions ?? []);
        return back()->with('success', __('Data successfully updated!'));
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Role $role)
    {
        $role->delete();
        return back()->with('success', __('Data successfully deleted!'));
    }

    /**
     * @return JsonResponse
     * @throws Exception
     */
    public function ajax()
    {
        $roles = Role::query();
        return DataTables::of($roles)->editColumn('name', function (Role $role) {
            return ucfirst($role->getAttribute('name'));
        })->addColumn('permissions', function (Role $role) {
            return view('app.admin.includes.table-elements.role-permissions', ['role' => $role]);
        })->addColumn('options', function (Role $role) {
            return view('app.admin.includes.table-elements.action-buttons', [
                'modelName' => 'role',
                'editUrl' => route('admin:roles.edit', $role->getAttribute('id')),
                'deleteUrl' => route('admin:roles.destroy', $role->getAttribute('id'))
            ]);
        })->toJson();
    }
}
