<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\User;
use App\ActivityLog;
use Illuminate\Support\Facades\Auth;
use Validator;
use Illuminate\Support\Facades\Mail;
use App\Mail\UserRegistration;
use Illuminate\Support\Facades\Hash;


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

        //where('status','Active')->
        $users = User::where('status','Active')->latest()->paginate(10);

        $data = [

            'users' => $users,
            'page_title' => 'Admin Users',
        ];
        return view('users.index',$data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $data = [
            'page_title' => 'New Admin User'
        ];
        return view('users.create', $data);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        \Log::info('here at the wall');
        // todo : form filler password : Pa$$w0rd!
        $this->validate($request, [
            'name' => 'required',
            'email'=> 'required|unique:users',
            'password' => 'required|confirmed'
           
        ]);
        // dd($request->all());
        // Hash::make($data['password'])

        $user = new User;
        $user->name = $request->name;
        $user->email = $request->email;
        $user->password = Hash::make($request->password) ;
        $user->save();

          //sending the mail
        $newUser = [
            'name' => $request->name,
            'email' => $request->email,
        ];
        $email = $request->email;
        Mail::to($email)->send(new UserRegistration($newUser));

        $desc = [
            'title' => 'Created a user',
            'data' => $request->name,
            'type' => 'user',
        ];

        $activity = new ActivityLog;
        $activity->user_id = Auth::user()->id;
        $activity->user_type = 'admin';
        $activity->description = json_encode($desc);
        $activity->save();

        return redirect()->to('/admin/users')->with('admin-sucsess', 'Successfully Added New User');
    }

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

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $user = User::findOrFail($id);
        $data = [
            'user' => $user,
             'page_title' => 'list_create'
        ];
        return view('users.update', $data);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $user = User::where('id', $id)->update([
            'status'=>'disabled',
        ]);
        
        $user = User::find($id);
        $desc = [
            'title'=> 'disabled a user with ID ' . $user->id,
            'data'=>  $user->name,
            'type'=>  'user',
        ];

        $activity = new ActivityLog;
        $activity->user_id = Auth::user()->id;
        $activity->user_type = 'admin'; //Auth::user()->id;
        $activity->description = json_encode($desc);
        $activity->save();

        return redirect()->to('/admin/users')->with('admin-sucsess', 'Successfully Disabled User');

    }
}
