<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

include 'core/autoload.php';
include 'core/app/model/StoreProductData.php';

// Usar Reflection para acceder a métodos privados
$reflection = new ReflectionClass('StoreProductData');

// Obtener conexión
$method = $reflection->getMethod('safeGetCon');
$method->setAccessible(true);
$con = $method->invoke(null);

// Obtener tabla
$method = $reflection->getMethod('resolveProductTable');
$method->setAccessible(true);
$productTable = $method->invoke(null, $con);

// Obtener field map
$method = $reflection->getMethod('getProductFieldMap');
$method->setAccessible(true);
$productFields = $method->invoke(null, $con, $productTable);

// Obtener SQL base  
$method = $reflection->getMethod('baseSelect');
$method->setAccessible(true);
$baseSql = $method->invoke(null, $con, $productTable, $productFields);

echo '<h2>Info Debuggeo</h2>';
echo '<p><strong>Tabla usado:</strong> ' . $productTable . '</p>';
echo '<p><strong>Field Map:</strong></p>';
echo '<pre>';
print_r($productFields);
echo '</pre>';

echo '<p><strong>SQL Base (primeras 500 chars):</strong></p>';
echo '<pre>';
echo htmlspecialchars(substr($baseSql, 0, 500));
echo '</pre>';

// Ejecutar una consulta simple
$sql = $baseSql . " ORDER BY id DESC LIMIT 3";
$result = $con->query($sql);
if ($result && $result->num_rows > 0) {
    echo '<p><strong>Resultados de consulta simple:</strong></p>';
    echo '<table border="1" cellpadding="5">';
    echo '<tr><th>id</th><th>name</th><th>stock_lapaz</th><th>stock_interior</th></tr>';
    while ($row = $result->fetch_assoc()) {
        echo '<tr>';
        echo '<td>' . $row['id'] . '</td>';
        echo '<td>' . substr($row['name'], 0, 30) . '</td>';
        echo '<td>' . ($row['stock_lapaz'] ?? 'NULL') . '</td>';
        echo '<td>' . ($row['stock_interior'] ?? 'NULL') . '</td>';
        echo '</tr>';
    }
    echo '</table>';
}
