<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;

class LogUserActivity
{
     public function handle($request, Closure $next)
    {
        if (Auth::check()) {
            $expiresAt = now()->addMinutes(2);
            Cache::put('user-is-online-' . Auth::id(), true, $expiresAt);

            // ✅ Update last_seen
            Auth::user()->forceFill(['last_seen' => now()])->save();
        }

        return $next($request);
    }
}
