<?php

namespace App\Http\Middleware;

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

class BusinessSetupComplete
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        $user = $request->user();

        // If user is authenticated and email is verified but no business setup
        if ($user && $user->hasVerifiedEmail() && !$user->business_id) {
            // Allow access to business routes and logout routes
            if (!$request->routeIs('business.*') && !$request->routeIs('logout')) {
                return redirect()->route('business.index')
                    ->with('warning', 'Please complete your business setup to continue.');
            }
        }

        return $next($request);
    }
}