<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

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

        if (auth()->check()) {
            $user = auth()->user();

            if ($user && method_exists($user, 'isDirty')) {
                $user->forceFill([
                    'last_activity_at' => now(),
                ])->save();
            }
        }

        return $response;
    }
}