<?php

namespace App\Http\Controllers\User;

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

class UserController extends Controller
{
    public function userDashboard()
    {
        return view('users.dashboard');
    }

    public function profile()
    {
        $users = User::find(auth::user()->id);
        return view('users.profile', compact('users'));
    }

    public function profileUpdate(Request $request)
    {
        // dd($request->all());
        $validator = Validator::make($request->all(),
        [
            'id' => 'required',
            'name' => 'required|max:255',
            'email' => 'nullable|email|max:255',
        ],
        [
            'name.required' => 'The name field is required',
            'name.max' => 'The name field enter only 255 characters',
            // 'email.required' => 'The email field is required',
            'email.max' => 'The email field enter only 255 characters',
        ]);

        if ($validator->fails()){
            return redirect('user/profile')->withErrors($validator)->withInput();
        }

        // Retrieve the validated input...
        $validated = $validator->validated();
        $profileupdate = User::find($request->id);
        $profileupdate->update($validated);
        return redirect('user/profile')->with('success', 'User profile updated successfully.');
    }
}
