<?php

declare(strict_types=1);

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Http\Requests\Auth\UpdateProfileRequest;
use App\Models\User;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

/**
 * A user's own account details.
 *
 * Name and email only. Role, status and password each have their own guarded
 * path, and none of them belongs on a form the account holder controls.
 */
class ProfileController extends Controller
{
    public function edit(Request $request): View
    {
        return view('auth.profile', [
            'user' => $request->user(),
        ]);
    }

    public function update(UpdateProfileRequest $request): RedirectResponse
    {
        /** @var User $user */
        $user = $request->user();

        // The Auditable trait records the change and its diff.
        $user->update($request->validated());

        return redirect()
            ->route('profile.edit')
            ->with('success', __('Your profile has been updated.'));
    }
}
