<?php

namespace App\Http\Middleware;

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

class BillerMiddleware
{
    /**
     * Handle an incoming request.
     *
     * @param  Closure(Request): (Response)  $next
     */
    public function handle(Request $request, Closure $next): Response
    {
        $user = auth()->user();

        if (! $user || $user->role !== 'biller' || ! $user->active) {
            auth()->logout();

            return redirect()->route('biller.login')->withErrors(['email' => 'Unauthorized access to Biller Panel.']);
        }

        return $next($request);
    }
}
