<?php

namespace App\Http\Middleware;

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

class SellerMiddleware
{
    /**
     * Handle an incoming request.
     */
    public function handle(Request $request, Closure $next): Response
    {
        $role = auth()->user()->role ?? 'user';
        
        // Allow Sellers, Admins, and Store Managers to access seller routes
        if (in_array($role, ['seller', 'admin', 'manager'])) {
            return $next($request);
        }

        // Redirect standard buyers back to their dashboard with an error
        return redirect()->route('dashboard.buyer')->withErrors(['error' => 'You do not have a seller account.']);
    }
}