<?php

namespace App\Http\Controllers\Admin;

use App\Constants\Status;
use App\Http\Controllers\Controller;
use App\Models\Admin;
use Illuminate\Http\Request;
use App\Models\Permission;
use App\Models\Role;
use App\Models\User;
use App\Rules\FileTypeValidate;
use Illuminate\Support\Facades\Hash;

class SpatiePermissionController extends Controller
{
    public function roles()
    {
        $pageTitle = 'Roles';
        $roles = Role::with('users', 'permissions')->searchable(['name'])->dateFilter('created_at')->paginate();
        return view('admin.access.roles', compact('pageTitle', 'roles'));
    }

    public function createEditRole($id = 0)
    {
        $pageTitle = 'Create Role';
        $role = Role::findOrNew($id);
        $permissions = Permission::all();
        $rolePermissions = $role->permissions()->pluck('id')->toArray();
        return view('admin.access.role-form', compact('pageTitle', 'role', 'permissions', 'rolePermissions'));
    }

    public function updateStoreRole(Request $request, $id = 0)
    {
        $role = Role::findOrNew($id);
        $role->name = $request->name;
        $role->save();
        $role->permissions()->sync($request->permissions);
        $notify[] = ['success', 'Role Saved'];
        return redirect()->route('admin.access.roles.index')->withNotify($notify);
    }

    public function permissions()
    {
        $pageTitle = 'Permissions';
        $permissions = Permission::with('roles')->searchable(['name'])->paginate();
        return view('admin.access.permissions', compact('pageTitle', 'permissions'));
    }


    public function viewPermission($id)
    {
        $pageTitle = 'View Permission';
        $permission = Permission::findOrFail($id);
        return view('admin.access.permission-view', compact('pageTitle', 'permission'));
    }

    public function employees()
    {
        $pageTitle = 'Employees';
        $users = Admin::employee()->with('roles')->with('roles')->searchable(['username', 'email'])->paginate();
        return view('admin.access.employees', compact('pageTitle', 'users'));
    }

    public function createEditEmployee(Request $request, $id = 0)
    {
        $roles = Role::all();
        $user = Admin::findOrNew($id);
        $userRoles = $user->roles()->pluck('id')->toArray();
        return view('admin.access.employees-form', compact('roles', 'user', 'userRoles'));
    }

    public function storeUpdateEmployee(Request $request, $id = 0)
    {
        $request->validate([
            'name' => 'required',
            'email' => 'required|email|unique:admins,email,' . $id,
            'mobile' => 'required|string',
            'username' => 'required|unique:admins,username,' . $id,
            'pass_code' => 'required',
            'image' => ['nullable', 'image', new FileTypeValidate(['jpg', 'jpeg', 'png'])]
        ]);

        $user = Admin::findOrNew($id);

        $user->name = $request->name;
        $user->email = $request->email;
        $user->mobile = $request->mobile;
        $user->username = $request->username;
        $user->pass_code = $request->pass_code;
        $user->password = Hash::make($user->pass_code);
        $user->status =  Status::USER_ACTIVE;

        if ($request->hasFile('image')) {
            try {
                $old = $user->image;
                $user->image = fileUploader($request->image, getFilePath('adminProfile'), getFileSize('adminProfile'), $old);
            } catch (\Exception $exp) {
                $notify[] = ['error', 'Couldn\'t upload your image'];
                return back()->withNotify($notify);
            }
        }

        $user->save();

        $notify[] = ['success', 'Employee Updated'];
        return redirect()->route('admin.access.employees')->withNotify($notify);
    }
    public function editRoleEmployee($id)
    {
        $user = Admin::findOrFail($id);
        $roles = Role::all();
        $userRoles = $user->roles()->pluck('id')->toArray();
        return view('admin.access.employee-role-form', compact('user', 'roles', 'userRoles'));
    }

    public function updateRoleEmployee(Request $request, $id)
    {
        $user = Admin::findOrFail($id);
        $user->roles()->sync($request->roles);

        $notify[] = ['success', 'Roles updated successfully'];
        return redirect()->route('admin.access.employees')->withNotify($notify);
    }


    public function banEmployee(Request $request, $id)
    {
        $user = Admin::findOrFail($id);

        $user->status = $request->status;
        $notify[] = ['success', $user->status == Status::USER_ACTIVE ? 'Employee Unbanned' : 'Employee Banned'];
        $user->save();
        return redirect()->route('admin.access.employees')->withNotify($notify);
    }
}
