<?php

namespace App\Http\Controllers\User;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class ProfileController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = Auth::user();
        return view('user.profile.index')->with([
            'user' => $user
        ]);   
    }


    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function edit(User $id)
    {  
        $user = Auth::user();
        return view('user.profile.edit')->with([
            'user' => $user
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, User $id)
    {
        $user = Auth::user();
        
        /**
         * Validate the form data
         */
        $validatedData = $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255'],
        ]);

        if($request->email != $user->email){
            $request->validate([
                'email' => ['unique:users'],
            ]);
        }

        /**
         * Validate password if it's being changed
         */
        if($request->filled('password')) {
            $validatedData = array_merge($validatedData, $request->validate([
                'password' => ['string', 'min:8'],
            ]));

            $user->password = Hash::make($validatedData['password']);
        }
      
        $user->name = $validatedData['name'];
        $user->email = $validatedData['email'];
        $user->save();
       
        return redirect()->route('user.profile.index')->with('success', 'Personal information successfully updated.');
    }
}
