<?php

namespace App\Http\Middleware;

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

class EnsureUserCanAccessActivationPages
{
    /**
     * Handle an incoming request.
     * Members and Leasing can always access these pages regardless of status.
     */
    public function handle(Request $request, Closure $next): Response
    {
        $user = $request->user();

        if (!$user) {
            return redirect()->route('login');
        }

        // Only leasing and member roles can access these
        if (!$user->isAdmin() && !$user->isLeasing() && !$user->isMember()) {
            abort(403, 'Akses ditolak.');
        }

        return $next($request);
    }
}
