<?php
/**
 * Role-based Access Control Middleware
 */
require_once __DIR__ . '/auth_middleware.php';

function role_middleware(string ...$allowedRoles): void
{
    auth_middleware();

    $currentRole = SessionManager::getUserRole();
    if (!in_array($currentRole, $allowedRoles)) {
        http_response_code(403);
        include APP_ROOT . '/templates/header.php';
        ?>
        <div class="main-content">
            <div class="container" style="padding:80px 20px;text-align:center">
                <div style="font-size:80px;color:#dc3545;margin-bottom:20px">&#9940;</div>
                <h2 style="color:#dc3545">403 - Access Denied</h2>
                <p style="color:#666;margin:15px 0 30px">You do not have permission to access this page.</p>
                <a href="<?= APP_URL ?>" class="btn btn-primary">Go to Dashboard</a>
            </div>
        </div>
        <?php
        include APP_ROOT . '/templates/footer.php';
        exit;
    }
}

function admin_only(): void
{
    role_middleware(ROLE_ADMIN);
}

function counter_only(): void
{
    role_middleware(ROLE_COUNTER);
}

function checker_only(): void
{
    role_middleware(ROLE_CHECKER);
}

function controller_only(): void
{
    role_middleware(ROLE_CONTROLLER);
}
