<?php
header('Content-Type: application/json');
require_once __DIR__ . '/../../app-api/config.php';

$db = getDB();

// Ver el registro del usuario mikels1
$stmt = $db->prepare("SELECT idusuarios_sist, nombre_usr, passwd, rango_idrango FROM usuarios_sist WHERE nombre_usr = ? LIMIT 1");
$stmt->execute(['mikels1']);
$user = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$user) {
    // Buscar usuarios similares
    $stmt2 = $db->query("SELECT idusuarios_sist, nombre_usr, rango_idrango FROM usuarios_sist LIMIT 10");
    $users = $stmt2->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode(['found' => false, 'available_users' => $users]);
} else {
    // Mostrar info del usuario y comparar hashes
    $testPass = 'Mikel123*';
    echo json_encode([
        'found' => true,
        'id' => $user['idusuarios_sist'],
        'nombre_usr' => $user['nombre_usr'],
        'rango' => $user['rango_idrango'],
        'passwd_length' => strlen($user['passwd']),
        'passwd_preview' => substr($user['passwd'], 0, 8) . '...',
        'md5_test' => md5($testPass),
        'match_Mikel123' => ($user['passwd'] === md5('Mikel123*')),
        'match_mikel123' => ($user['passwd'] === md5('mikel123*')),
        'match_Mikel123_lower' => ($user['passwd'] === md5('Mikel123')),
        'match_123456'           => ($user['passwd'] === md5('123456')),
        'match_mikels1'          => ($user['passwd'] === md5('mikels1')),
    ]);
}
