<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules\Password;
use App\Services\ActionNotificationService;

class PasswordController extends Controller
{
    /**
     * Update the user's password.
     */
    public function update(Request $request, ActionNotificationService $notifications): RedirectResponse
    {
        $validated = $request->validateWithBag('updatePassword', [
            'current_password' => ['required', 'current_password'],
            'password' => ['required', Password::defaults(), 'confirmed'],
        ]);

        $request->user()->update([
            'password' => Hash::make($validated['password']),
        ]);

        $notifications->notifyAction(
            'Password Changed',
            $request->user(),
            [
                'channel' => 'web',
            ]
        );

        return back()->with('status', 'password-updated');
    }
}
