<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\RoleModel;
use App\Models\Roles2Model;
use App\Models\PermissionsModel;
use App\Models\PermissionRolesModel;

class Roles extends Controller
{
    public function list(){
        $data = RoleModel::getRecords();
        return view("roles.list", compact("data"));
    }
    public function add(){
        $getPermissions = PermissionsModel::getRecords();
        $data['getPermissions'] = $getPermissions;
        
        return view("roles.add", $data);

    }
    public function addRole(){
        $getRoles = Roles2Model::getRecords();
        $data['getRoles'] = $getRoles;
        return view("roles.add", $data);

    }
    public function insertRole(Request $request){
        $save = new Roles2Model();
        $save->name = $request->roleName;
        $save->description = $request->description;
        $save->save();
        
        return redirect('users/role')->with('success', 'Role added Successfully');
    }


    public function updateRole($id, Request $request)
    {
        // Validate the request data
        $validatedData = $request->validate([
            'roleName' => 'required|string',
            'description' => 'required|string',
        ]);
        $role = Roles2model::findOrFail($id);
    
        $role->name = $validatedData["roleName"];
        $role->description = $validatedData["description"];
        
        $role->save();

        return redirect('users/role')->with('success', 'Role updated successfully!');

    }
    public function deleteRole($id)
    {
        try {
            $user = Roles2Model::findOrFail($id);
            $user->delete();

            return redirect('users/role')->with('success', 'Role deleted successfully!');
        } catch (\Exception $e) {
            return redirect()->back()->with('error', 'An error occurred: ' . $e->getMessage());
        }
    }


    public function insert(Request $request){
        $save = new RoleModel();
        $save->name = $request->roleName;
        $save->save();

        PermissionRolesModel::InsertUpdateRecord($request->permission_id, $save->id);

        return redirect('users/role')->with('success', 'Role added Successfully');
    }



    public function edit($id){
        $getPermissions = PermissionsModel::getRecords();
        $data['getPermissions'] = $getPermissions;
        $data['getRolesPermissions'] = PermissionRolesModel::getRolePermissions($id);
        $data['getRecord'] = RoleModel::getRecord($id);
        return view("roles.edit", $data ,compact("id"));

    }
    public function update($id, Request $request){
        $save = RoleModel::getRecord($id);
        $save->name = $request->roleName;
        $save->save();
        PermissionRolesModel::InsertUpdateRecord($request->permission_id, $save->id);

        return redirect('users/role')->with('success', 'Role updated Successfully');
    }

    
    public function delete($id){
        $save = RoleModel::getRecord($id);
        $save->delete();

        return redirect('users/role')->with('success', 'Role deleted Successfully');
    }


}
