<?php

require_once "{$_SERVER['DOCUMENT_ROOT']}/class/c_login.php";
header('Content-Type: application/json');

if (!Login::is_logged()) {
    header('HTTP/1.1 401 Unauthorized');
    echo json_encode(['error' => 'No se ha iniciado sesión']);
    exit();
}
$user = Login::get_user();

try {
    switch ($_SERVER['REQUEST_METHOD']) {
        case 'GET':
            // Fetch all puestos
            $nivel = $db->get('nivel');
            echo json_encode($nivel);
            break;
        default:
            header('HTTP/1.1 405 Method Not Allowed');
            echo json_encode(['error' => 'Método no permitido']);
            break;
    }
} catch (PDOException $e) {
    echo json_encode([
        'error' => $e->getMessage(),
        'query' => $db->getLastQuery(),
        'exception' => $e->getTraceAsString()
    ]);
}