<?php

namespace App\Http\Middleware;

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

class EmployerMiddleware
{
    /**
     * Handle an incoming request.
     */
    public function handle(Request $request, Closure $next): Response
    {
        $user = auth()->check() ? auth()->user() : session('user');
        $role = is_array($user) ? ($user['role'] ?? null) : ($user->role ?? null);
        if (!$user || $role !== 'employer') {
            return redirect()->route('employer.login');
        }

        return $next($request);
    }
}
