<?php

namespace App\Http\Controllers\V3\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class LogoutController extends Controller
{
    use AuthenticatesUsers;
    /**
     * Handle the incoming request.
     */
    public function __invoke(Request $request): JsonResponse
    {
        if($request->session()->exists('last_login')){
            $user = auth()->user();
            $user->last_login = $request->session()->get('last_login');
            $user->save();
            $request->session()->forget('last_login');
        }

        $this->guard()->logout();

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

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

        return response()->json([
            'message' => 'logged out'
        ]);
    }
}
