<?php

namespace App\Http\Controllers\API\v1\Admin;

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

class LogoutController extends Controller
{
    public function __invoke(Request $request)
    {
        $user = Auth::user();

        if ($user) {
            activity()
                ->performedOn($user)
                ->withProperties([
                    'admin_id' => $user->id,
                    'admin_name' => $user->name,
                    'admin_email' => $user->email,
                    'admin_role' => $user->role,
                    'ip' => $request->ip(),
                    'user_agent' => $request->userAgent(),
                ])
                ->log("admin logout: {$user->email}");
        } else {
            activity()
                ->withProperties([
                    'ip' => $request->ip(),
                    'user_agent' => $request->userAgent(),
                ])
                ->log('admin logout');
        }

    	Auth::guard('web')->logout();
    	Auth::guard('voter')->logout();
    	return response()->json([
    		'status'  =>  'success',
    		'message'  => 'Logout successfully',
    		'user'  => Auth::user()
    	]);
    }
}