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

$results = [];

// Probar ruta 1: ../../config.php (raíz)
$path1 = __DIR__ . '/../../config.php';
$results['path1'] = $path1;
$results['path1_exists'] = file_exists($path1);

// Probar ruta 2: ../../app-api/config.php
$path2 = __DIR__ . '/../../app-api/config.php';
$results['path2'] = $path2;
$results['path2_exists'] = file_exists($path2);

// Probar ruta 3: ../config.php
$path3 = __DIR__ . '/../config.php';
$results['path3'] = $path3;
$results['path3_exists'] = file_exists($path3);

// Usar la que funcione
if (file_exists($path1)) {
    require_once $path1;
    $results['using'] = 'path1';
} elseif (file_exists($path2)) {
    require_once $path2;
    $results['using'] = 'path2';
} elseif (file_exists($path3)) {
    require_once $path3;
    $results['using'] = 'path3';
} else {
    $results['error'] = 'No config found';
    echo json_encode($results);
    exit;
}

try {
    $db = getDB();
    // Buscar el usuario admin
    $stmt = $db->query("SELECT idusuarios_sist, nombre_usr, rango_idrango FROM usuarios_sist LIMIT 5");
    $results['usuarios'] = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (Exception $e) {
    $results['db_error'] = $e->getMessage();
}

echo json_encode($results, JSON_PRETTY_PRINT);
