<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Log;

class LogEndpointHitMiddleware {
    public function handle($request, Closure $next) {
        $user = $request->user;
        $method = $request->getMethod();
        $path = $request->getPathInfo();

        Log::debug('User ' . $user->id . ' hitting ' . $method . ' ' . $path . ' endpoint', [
            'user' => $user->id,
            'roles' => implode('|', $user->roles),
            'method' => $method,
            'path' => $path,
            'input' => array_filter($request->input(), function ($key) { return $key !== 'user'; }, ARRAY_FILTER_USE_KEY)
        ]);

        return $next($request);
    }
}
