<?php

namespace App\Http\Middleware;

use App\Services\Meet\OrganizationResolver;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

class EnsureOrganizationSetup
{
    public function __construct(
        protected OrganizationResolver $organizations,
    ) {}

    public function handle(Request $request, Closure $next): Response
    {
        $user = $request->user();
        if (! $user) {
            return $next($request);
        }

        if ($request->routeIs('meet.onboarding*')) {
            return $next($request);
        }

        if (! $this->organizations->isOnboarded($user)) {
            return redirect()->route('meet.onboarding.show');
        }

        return $next($request);
    }
}
