<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

if (!isset($_SESSION['access_level'])) {
    $_SESSION['access_level'] = 'Member';
}

function accessHierarchy()
{
    return [
        'Member' => 1,
        'Training' => 2,
        'Admin' => 3,
        'SuperAdmin' => 4
    ];
}

function currentAccessLevel()
{
    return $_SESSION['access_level'] ?? 'Member';
}

function hasAccess($required_level)
{
    $hierarchy = accessHierarchy();

    $current = currentAccessLevel();

    $current_rank = $hierarchy[$current] ?? 1;
    $required_rank = $hierarchy[$required_level] ?? 1;

    return $current_rank >= $required_rank;
}

function requireAccess($required_level)
{
    if (!hasAccess($required_level)) {

        header("Location: dashboard.php");
        exit;
    }
}
?>