<?php

if (!function_exists('is_superadmin')) {

    function is_superadmin(): bool
    {
        if (!isset($_SESSION['user_roles'])) {
            return false;
        }

        foreach ($_SESSION['user_roles'] as $role) {

            if (
                strtolower($role['code'] ?? '') === 'superadmin'
                || intval($role['id'] ?? 0) === 1
            ) {
                return true;
            }
        }

        return false;
    }

}

if (!function_exists('has_permission')) {

    function has_permission(string $permission): bool
    {
        /*
         GLOBAL OVERRIDE
        */

        if (is_superadmin()) {
            return true;
        }

        if (!isset($_SESSION['permissions'])) {
            return false;
        }

        return in_array($permission, $_SESSION['permissions']);
    }

}
