<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Response;

class LogUserActivity
{
    public function handle(Request $request, Closure $next): Response
    {
        $response = $next($request);

        if ($request->user() && !$request->ajax() && $request->method() === 'GET') {
            DB::table('users')
                ->where('id', $request->user()->id)
                ->update(['last_active_at' => now()]);
        }

        return $response;
    }
}
