<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;

class EnsureCentralDomain
{
    public function handle(Request $request, Closure $next)
    {
        $allowed = array_map(
            fn ($entry) => parse_url((string) $entry, PHP_URL_HOST) ?? $entry,
            (array) config('tenancy.central_domains')
        );

        if (! in_array($request->getHost(), $allowed, true)) {
            abort(404);
        }

        return $next($request);
    }
}
