<?php
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');

// Activar reporte de errores
ini_set('display_errors', 1);
error_reporting(E_ALL);

$results = [];

// 1. Probar config
try {
    require_once __DIR__ . '/../../config.php';
    $results['config'] = 'OK';
} catch (Throwable $e) {
    $results['config_error'] = $e->getMessage();
    echo json_encode($results);
    exit;
}

// 2. Probar getDB
try {
    $db = getDB();
    $results['db'] = 'OK';
} catch (Throwable $e) {
    $results['db_error'] = $e->getMessage();
    echo json_encode($results);
    exit;
}

// 3. Probar tabla voucher_grupal
try {
    $stmt = $db->query("SHOW TABLES LIKE 'voucher_grupal'");
    $exists = $stmt->fetchColumn();
    $results['tabla_voucher_grupal'] = $exists ? 'EXISTS' : 'NOT FOUND';
} catch (Throwable $e) {
    $results['tabla_error'] = $e->getMessage();
}

// 4. Probar tabla inversionista
try {
    $stmt = $db->query("SELECT COUNT(*) FROM inversionista LIMIT 1");
    $results['inversionista_count'] = $stmt->fetchColumn();
} catch (Throwable $e) {
    $results['inversionista_error'] = $e->getMessage();
}

// 5. Probar tabla admin_sessions
try {
    $stmt = $db->query("SHOW COLUMNS FROM admin_sessions");
    $cols = $stmt->fetchAll(PDO::FETCH_COLUMN);
    $results['admin_sessions_cols'] = $cols;
} catch (Throwable $e) {
    $results['admin_sessions_error'] = $e->getMessage();
}

// 6. Probar getallheaders
$results['getallheaders_exists'] = function_exists('getallheaders');

echo json_encode($results, JSON_PRETTY_PRINT);
