<?php

declare(strict_types=1);

namespace App\Http\Controllers\Account;

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

class SessionController extends Controller
{
    public function destroy(Request $request): RedirectResponse
    {
        $request->validate([
            'password' => ['required', 'string'],
        ]);

        if (! Hash::check($request->input('password'), $request->user()->password)) {
            return redirect()->back()->withErrors([
                'password' => 'The provided password is incorrect.',
            ]);
        }

        Auth::logoutOtherDevices($request->input('password'));

        return redirect()->back()->with('success', 'All other sessions have been logged out.');
    }
}
