<?php
// DEBUG EXTREMO - Identificar el problema específico
error_reporting(E_ALL);
ini_set('display_errors', 1);
header('Content-Type: application/json');

$debug_info = [];
$debug_info['step'] = 'inicio';
$debug_info['timestamp'] = date('Y-m-d H:i:s');

try {
    $debug_info['step'] = 'incluir_config';
    
    // Verificar si el archivo de configuración existe
    $config_file = __DIR__ . '/../config/db_auto.php';
    $debug_info['config_file_exists'] = file_exists($config_file);
    $debug_info['config_file_path'] = $config_file;
    
    if (!$debug_info['config_file_exists']) {
        throw new Exception("Archivo de configuración no encontrado: $config_file");
    }
    
    require_once $config_file;
    $debug_info['step'] = 'config_incluida';
    
    // Verificar si la variable $pdo existe
    $debug_info['pdo_exists'] = isset($pdo);
    $debug_info['pdo_type'] = isset($pdo) ? get_class($pdo) : 'no existe';
    
    if (!isset($pdo)) {
        throw new Exception("Variable \$pdo no está definida después de incluir configuración");
    }
    
    $debug_info['step'] = 'test_conexion';
    
    // Test básico de conexión
    $test_query = $pdo->query("SELECT 1 as test");
    $test_result = $test_query->fetch();
    $debug_info['connection_test'] = $test_result ? 'exitosa' : 'fallida';
    
    $debug_info['step'] = 'test_tabla_alumnos';
    
    // Verificar si existe la tabla alumnos
    $table_check = $pdo->query("SHOW TABLES LIKE 'alumnos'");
    $debug_info['tabla_alumnos_exists'] = $table_check->rowCount() > 0;
    
    if ($debug_info['tabla_alumnos_exists']) {
        $debug_info['step'] = 'contar_alumnos';
        $count_query = $pdo->query("SELECT COUNT(*) as total FROM alumnos");
        $count_result = $count_query->fetch();
        $debug_info['total_alumnos'] = $count_result['total'];
    }
    
    $debug_info['step'] = 'parametros_get';
    $debug_info['get_params'] = $_GET;
    
    $debug_info['status'] = 'success';
    $debug_info['message'] = 'Todas las verificaciones pasaron correctamente';
    
} catch (Exception $e) {
    $debug_info['status'] = 'error';
    $debug_info['error_message'] = $e->getMessage();
    $debug_info['error_file'] = $e->getFile();
    $debug_info['error_line'] = $e->getLine();
    http_response_code(500);
}

echo json_encode($debug_info, JSON_PRETTY_PRINT);
?>
