<?php
// Direct DB connection test - no JSON, no API, just raw DB
header('Content-Type: application/json; charset=utf-8');

error_reporting(E_ALL);
ini_set('display_errors', 0);

// Load database config directly
$config_file = __DIR__ . '/db_config.php';
if (!file_exists($config_file)) {
    die(json_encode(['error' => 'db_config.php not found']));
}

include $config_file;

$result = [
    'db_config_included' => true,
    'conn_exists' => isset($conn),
    'conn_is_object' => is_object($conn ?? null),
    'conn_error' => ($conn && method_exists($conn, 'connect_error')) ? $conn->connect_error : 'N/A',
    'users' => [],
    'error' => null
];

if ($conn && !$conn->connect_error) {
    // Try simple query
    $query = 'SELECT id, username, email FROM users LIMIT 5';
    $res = $conn->query($query);
    
    if ($res) {
        $result['query_success'] = true;
        $result['row_count'] = $res->num_rows;
        
        while ($row = $res->fetch_assoc()) {
            $result['users'][] = $row;
        }
    } else {
        $result['query_success'] = false;
        $result['query_error'] = $conn->error;
    }
} else {
    $result['error'] = 'No valid database connection';
}

echo json_encode($result, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
?>
