<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
header('Content-Type: text/plain; charset=utf-8');
header('Access-Control-Allow-Origin: *');

require_once __DIR__ . '/../../config.php';

$token = '69159f785a6c674d06e74343635303478fdff178a37ccea42ec40f3b070a150f';
$db  = getDB();
$now = date('Y-m-d H:i:s');

echo "Token: " . substr($token, 0, 20) . "...\n";
echo "Now: $now\n";

$stmtSess = $db->prepare("SELECT idusuarios_sist FROM admin_sessions WHERE token = ? AND expira > ? LIMIT 1");
$stmtSess->execute([$token, $now]);
$sess = $stmtSess->fetch();
echo "Sesion: " . ($sess ? "OK uid=" . $sess['idusuarios_sist'] : "NO ENCONTRADA") . "\n";

// Probar busqueda
$q = '%maria%';
$stmt = $db->prepare("
    SELECT id_inver, referencia, rfc,
           CONCAT(COALESCE(nombre,''), ' ', COALESCE(apellidop,''), ' ', COALESCE(apellidom,'')) AS nombre_completo
    FROM inversionista
    WHERE nombre LIKE ? OR apellidop LIKE ? OR referencia LIKE ? OR rfc LIKE ?
    LIMIT 10
");
$stmt->execute([$q, $q, $q, $q]);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
echo "Resultados: " . count($rows) . "\n";
foreach ($rows as $r) {
    echo "  - " . $r['referencia'] . " " . trim($r['nombre_completo']) . "\n";
}
