<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class ProfileController extends Controller
{
    public function index()
    {
        return view('web.themes.shella.pages.profile.index');
    }

    public function edit()
    {
        return view('web.profile.edit');
    }

    public function update(Request $request)
    {
        $data = $request->validate([
            'first_name' => 'required|string',
            'last_name' => 'required|string',
            'password' => 'nullable|min:6',
            'phone_number' => ['required', 'integer', 'between:61000000,65999999', Rule::unique('users', 'phone_number')->ignore(auth()->id())],
            'email' => ['required', 'email', Rule::unique('users', 'email')->ignore(auth()->id())],
        ]);

        if (isset($data['password'])) {
            $data['password'] = bcrypt($data['password']);
        }

        auth()->user()->update(array_diff($data, [null]));

        return redirect()->route('web.profile.index');
    }
}
