<?php

namespace App\Http\Controllers;

use Illuminate\Routing\Controllers\Middleware;
use Illuminate\Routing\Controllers\HasMiddleware;
use Illuminate\Http\Request;
use App\Models\Config;
use App\Models\Role;
use App\Models\Permission;
use Illuminate\Support\Facades\Auth;
use Notification;
use Session;
use PDF;
use Illuminate\Support\Str;
use Carbon\Carbon;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\ImageManager;
use Intervention\Image\Drivers\Imagick\Driver;
use Illuminate\Support\Facades\Log;
use App\DataTables\RoleDataTable;

class RoleController extends Controller implements HasMiddleware
{
  public static $permission_group = "Ruoli e Permessi";
  public static $permissions = [
    'view-role' => 'Vedi',
    'create-role' => 'Crea',
    'edit-role' => 'Modifica',
    'remove-role' => 'Elimina',
  ];

  public static function middleware(): array
  {
    return [
      new Middleware('permission:view-role', only: ['index']),
      new Middleware('permission:create-role|edit-role|remove-role', only: ['store']),
    ];
  }

  public function index(RoleDataTable $dataTable)
  {
    return $dataTable->render('role.index');
  }

  public function showEdit(Request $request){
    if(!$request->has('role_id') || !$request->role_id){
      return response()->json(['error' => 'ID ruolo non specificato'], 400);
    }
    
    $role = Role::find($request->role_id);
    
    if(!$role){
      return response()->json(['error' => 'Ruolo non trovato'], 404);
    }
    
    return view('role.modal_edit', ['role' => $role]);
  }

  public function showNew(Request $request){
    return view('role.modal_new');
  }

  public function updatePermission(Request $request){
    $input = $request->all();
    // dd($input);
    $permissions = [];
    $role = Role::find($input['role_id']);
    // $role->display_name = $request->modalRoleName;
    // $role->name = Str::slug($role->display_name);
    $role->save();

    if($request->has('permesso')){
      foreach($input['permesso'] as $key => $value){
        $p = Permission::find($value);
        if($p != null){
          array_push($permissions, $p->name);
        }
      }

    }

    $role->syncPermissions($permissions);

    $request->session()->flash('status', 'Permessi aggiornati!');
    return redirect()->route('role.index');
  }

  public function createRole(Request $request){
    $input = $request->all();
    $permissions = [];
    $role = new Role;
    $role->display_name = $request->modalRoleName;
    $role->guard_name = 'web';
    $role->name = Str::slug($role->display_name);;
    $role->save();

    if($request->has('permesso')){
      foreach($input['permesso'] as $key => $value){
        $p = Permission::find($value);
        if($p != null){
          array_push($permissions, $p->name);
        }
      }

    }

    $role->syncPermissions($permissions);

    $request->session()->flash('status', 'Ruolo creato!');
    return redirect()->route('user.index');
  }

  public function getPermissions(Request $request){
    if(!$request->has('role_id') || !$request->role_id){
      return response()->json(['error' => 'ID ruolo non specificato'], 400);
    }
    
    $role = Role::find($request->role_id);
    
    if(!$role){
      return response()->json(['error' => 'Ruolo non trovato'], 404);
    }
    if($request->has('modal-edit')){
      return view('role.permessi', ['role' => $role]);
    }
    return response()->json(['permissions' => $role->permissions]);
  }

}
