<?php
declare(strict_types=1);

error_reporting(E_ALL);
ini_set('display_errors','1');
ini_set('display_startup_errors','1');

register_shutdown_function(function () {
  $e = error_get_last();
  if ($e) {
    echo "<pre style='white-space:pre-wrap;font:14px/1.4 monospace'>ERROR FATAL:\n";
    print_r($e);
    echo "</pre>";
  }
});

set_exception_handler(function(Throwable $e){
  echo "<pre style='white-space:pre-wrap;font:14px/1.4 monospace'>EXCEPCIÓN:\n";
  echo $e->getMessage()."\n\n".$e->getTraceAsString();
  echo "</pre>";
});

require_once __DIR__ . '/../app/auth.php';
require_once __DIR__ . '/../app/db.php';

start_session();

echo "<pre>";
echo "PHP: " . PHP_VERSION . "\n";
echo "SESSION uid: " . ($_SESSION['uid'] ?? 'NULL') . "\n";
echo "SESSION username: " . ($_SESSION['username'] ?? 'NULL') . "\n";
echo "SESSION roles: ";
print_r($_SESSION['roles'] ?? []);
echo "\n";

$pdo = db();
echo "DB OK ✅\n";

echo "has_role('ADMIN'): " . (has_role('ADMIN') ? 'SI' : 'NO') . "\n";
echo "has_role('DOCENTE'): " . (has_role('DOCENTE') ? 'SI' : 'NO') . "\n";
echo "</pre>";
