<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\DB;
use App\Models\ObjectModel;
use App\Models\UserObject;
use Illuminate\Queue\Events\Looping;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function index()
    {

        if(Auth::user()->type === 'admin'){

            $users = User::where('id','!=',Auth::user()->id)->paginate(2);
            return view('user.index', compact('users'));
        }else {
            return view('unauthorized');
        }
        
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        if(Auth::user()->type === 'admin'){
            return view('user.create');
        }else {
            return view('unauthorized');
        }
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {

        if(Auth::user()->type === 'admin'){

            $user = new User; 
            $user->name=$request->name; 
            $user->email=$request->email; 
            $user->password= Hash::make($request->password);
            $user->type=$request->type; 
            $user->save();
            return back()->withSuccess('User Created Succesfully');
        }else {
            return view('unauthorized');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        if(Auth::user()->type === 'admin'){
            $user = User::where('id',$id)->first();
            return view('user.edit',compact('user'));
        }else {
            return view('unauthorized');
        }
    }


    public function object($id)
    {

        if(Auth::user()->type === 'admin'){
            $userObjects = UserObject::where('user_id', $id)->with('object')->get();
            $objects = ObjectModel::get();
            $user = User::where('id', $id)->first();

            $allObjects = [];
            foreach ($objects as $key => $object) {

                $is_user_object = false;
                foreach ($userObjects as $userObject) {
                    
                    if($object->id == $userObject->object_id){
                        $is_user_object = true;
                    }
                }

                if($is_user_object == true){
                    array_push($allObjects, [
                        'id' => $object->id,
                        'name' => $object->name,
                        'checked' => 'checked',
                    ]);
                }else{
                    array_push($allObjects, [
                        'id' => $object->id,
                        'name' => $object->name,
                        'checked' => '',
                    ]);
                }
            }

            return view('user.object',compact('allObjects','id', 'user'));

        }else {
            return view('unauthorized');
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if(Auth::user()->type === 'admin'){
            $user = User::where('id',$id)->update([
                'name' => $request->name,
                'email' => $request->email,
                'password' => Hash::make($request->password),
                'type' => $request->type,
            ]);
            
        
            return redirect()->route('user.index')->withSuccess('User Updated Succesfully');
        }
    }

    public function object_update(Request $request, $user_id)
    {
        if(Auth::user()->type === 'admin'){
        
            UserObject::where('user_id', $user_id)->delete();

            foreach($request->checked as $check){
                $user_object_relation = UserObject::create([
                    'user_id' => $user_id,
                    'object_id' => $check,
                ]);
            }

            return back()->withSuccess('User object assigned succesfully');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if(Auth::user()->type === 'admin'){
            $user = User::where('id',$id)->delete();
        
            return back()->withSuccess('User Deleted Succesfully');
        }
    }
}
