<?php

namespace App\Http\Controllers\Auth;

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

class LogoutController extends Controller
{
    use FlashesEntityMessage;

    public function store(Request $request): RedirectResponse
    {
        if ($user = $request->user()) {
            activity()
                ->causedBy($user)
                ->event('logout')
                ->log('Keluar dari aplikasi');
        }

        Auth::logout();

        $request->session()->invalidate();
        $request->session()->regenerateToken();

        $this->flashSuccess('Anda berhasil keluar.');

        return redirect()->route('login');
    }
}
