<?php

namespace Root\Core\Middleware;

use Closure;
use Illuminate\Http\Request;

class CheckInternalApiKey
{
    public function handle(Request $request, Closure $next)
    {
        $expectedKey = CoreConfig('app.apiKey');

        $incomingKey = $request->header('X-Internal-Api-Key');

        if (!$incomingKey || $incomingKey !== $expectedKey) {
            return response()->json([
                'message' => 'Unauthorized - Invalid Internal API Key'
            ], 401);
        }

        return $next($request);
    }
}
