<?php
function jsonResponse(array $data, int $status=200): never {
    http_response_code($status);
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($data, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);
    exit;
}
function bodyJson(): array {
    $raw=file_get_contents('php://input');
    if(!$raw) return $_POST;
    $data=json_decode($raw,true);
    return is_array($data)?$data:$_POST;
}
function requireAdmin(): array {
    if(session_status()!==PHP_SESSION_ACTIVE) session_start();
    if(empty($_SESSION['novax_user']) || ($_SESSION['novax_user']['rol']??'')!=='administrador') {
        jsonResponse(['ok'=>false,'mensaje'=>'Acceso solo para administradores.'],401);
    }
    return $_SESSION['novax_user'];
}
