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

if (empty($_SESSION['login']) || empty($_SESSION['role'])) {
    session_unset();
    session_destroy();
    header('Location: ../index.php');
    exit;
}

function user_role() {
    return $_SESSION['role'] ?? '';
}

function is_admin() {
    return user_role() === 'admin';
}

function is_guru() {
    return user_role() === 'guru';
}

function require_role($roles) {
    $roles = (array) $roles;
    if (!in_array(user_role(), $roles, true)) {
        if (user_role() === 'siswa') {
            header('Location: ../siswa/status_lulus.php');
        } else {
            header('Location: ../auth/logout.php');
        }
        exit;
    }
}
?>
