<?php

namespace App\Http\Middleware;

use App\Tenancy\TenantManager;
use Closure;
use Illuminate\Http\Request;

/**
 * If the current tenant has never completed the onboarding wizard,
 * redirect logged-in users to /app/onboarding.
 *
 * Skip when:
 * - already on /app/onboarding
 * - on /app/login or /app/logout
 * - on /livewire/* (XHR — wizard handles its own POST)
 */
class RequireOnboarding
{
    public function handle(Request $request, Closure $next)
    {
        $path = $request->path();

        // Skip non-tenant-panel paths.
        if (! str_starts_with($path, 'app')) {
            return $next($request);
        }

        // Skip auth & onboarding paths.
        if (
            str_contains($path, 'login')
            || str_contains($path, 'logout')
            || str_contains($path, 'onboarding')
            || str_starts_with($path, 'livewire')
            || str_starts_with($path, 'filament')
        ) {
            return $next($request);
        }

        $tenant = app(TenantManager::class)->current();
        if (! $tenant) {
            return $next($request);
        }

        // Only redirect authenticated users.
        if (! auth('web')->check()) {
            return $next($request);
        }

        if (empty($tenant->settings['onboarded_at'])) {
            return redirect('/app/onboarding');
        }

        return $next($request);
    }
}
