<?php

namespace App\Http\Middleware;

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

use App\Models\CompanySetting;

class ServerNameCheckMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    // public function handle(Request $request, Closure $next): Response
    // {
    //     return $next($request);
    // }

    public function handle(Request $request, Closure $next): Response
    {
        $serverName = md5($request->route('ServerName'));
        $companySettings = CompanySetting::first();

        if (!$companySettings || $serverName != $companySettings->ServerName) {
            return response()->json(['status' => false, 'message' => 'Server does not match'], 403);
        }

        return $next($request);
    }

}
