<?php

namespace App\Http\Middleware;

use Closure;
use DateTime;
use DateTimeInterface;
use DB;

class LogRequest
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {   
        // $time = floor(time() / 1800); // time in seconds, divided by 1800 seconds (30 minutes)
        // if(DB::table('logs')->where('time', $time)->count() > 0){
        //     DB::table('logs')->where('time', $time)->increment('requests');
        // }else{
        //     DB::table('logs')->insert(['time' => $time, 'requests' => 1]);
        // }

        // If the request is authenticated, check the user's username against the list of tracked usernames.
        $reqUser = \newGetUser(true);
        if (!is_null($reqUser) && $request->path()) {
            $username = $reqUser->username;

            if (isUsernameOnTrackedUsernamesList($username)) {
                logUserTrackingMessage("User \"$username\" visited a page:\n" . createRequestInfoDump($request));
            }
        }

        return $next($request);
    }
}
