<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpFoundation\Response;

class ResellerMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        if (!Auth::guard('resellers')->check()) {
            return redirect('/');
        }

        $reseller = Auth::guard('resellers')->user();

        if ($reseller->status !== 'active') {
            Auth::guard('resellers')->logout();
            return redirect('/')->with('error', 'Your account is not active.');
        }

        return $next($request);
    }
}