<?php

namespace App\Http\Middleware;

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

class ComingSoon
{
    /**
     * Handle an incoming request.
     *
     * @param  Closure(Request): (Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if (config('app.coming_soon', false) || env('COMING_SOON', false)) {
            $allowedPaths = [
                'coming-soon',
                'feedback',
                'admin*',
                'livewire*',
                'up',
                'webhooks*',
                '_debugbar*',
            ];

            $isAllowed = false;
            foreach ($allowedPaths as $path) {
                if ($request->is($path)) {
                    $isAllowed = true;
                    break;
                }
            }

            if (!$isAllowed) {
                return redirect()->route('coming-soon');
            }
        }

        return $next($request);
    }
}
