<?php
require_once 'config.php';
require_once 'auth.php';
checkLogin();

$status = "";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $buque_id = $_POST['buque_id'] ?? 'BOUCIÑA-01';
    $temp = $_POST['temp'] ?? rand(10, 30);
    $sensor_id = $_POST['sensor_id'] ?? rand(1, 15);
    
    // Formato exacto del ESP32: temp,idZona,timestamp,apiKey,buqueId
    // Nota: El ESP32 original manda 4 valores y el buqueId queda por defecto, 
    // pero la API soporta el 5to valor para identificar el buque.
    $payload = "$temp,$sensor_id,now," . API_KEY . ",$buque_id";
    
    // Forzamos http:// para que la prueba sea IDÉNTICA a la del ESP32 físico
    $url = "http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/api/buque.php";
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    
    $status = "Enviado: <strong>$payload</strong><br>Respuesta: $response (HTTP $httpCode)";
}

$pdo = getDBConnection();
$ships = [];
if ($pdo) {
    $stmt = $pdo->query("SELECT ship_id, name FROM ships");
    $ships = $stmt->fetchAll();
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Simulador de Buque ESP32</title>
    <style>
        body { font-family: sans-serif; background: #1a1a2e; color: white; padding: 40px; text-align: center; }
        .card { background: #16213e; padding: 20px; border-radius: 10px; display: inline-block; text-align: left; border: 1px solid #4ecca3; }
        select, input, button { padding: 10px; margin: 5px 0; width: 100%; box-sizing: border-box; }
        button { background: #4ecca3; border: none; font-weight: bold; cursor: pointer; color: #1a1a2e; }
        .status { margin-top: 20px; padding: 10px; background: #0f3460; border-radius: 5px; font-family: monospace; }
    </style>
</head>
<body>
    <h1>Simulador de Envío ESP32</h1>
    <p>Usa esta herramienta para enviarle datos de prueba a la API y verlos en el Dashboard.</p>
    
    <div class="card">
        <form method="POST">
            <label>Seleccionar Buque:</label>
            <select name="buque_id">
                <?php foreach ($ships as $s): ?>
                    <option value="<?php echo htmlspecialchars($s['ship_id']); ?>">
                        <?php echo htmlspecialchars($s['name']); ?> (<?php echo htmlspecialchars($s['ship_id']); ?>)
                    </option>
                <?php endforeach; ?>
            </select>
            
            <label>Temperatura (°C):</label>
            <input type="number" name="temp" step="0.1" value="22.5">
            
            <label>ID Sensor (1-15):</label>
            <input type="number" name="sensor_id" value="1" min="1" max="15">
            
            <button type="submit">SIMULAR ENVÍO</button>
        </form>
        
        <?php if ($status): ?>
            <div class="status">
                <?php echo $status; ?>
            </div>
        <?php endif; ?>
    </div>
    
    <br><br>
    <a href="dashboard.php" style="color: #4ecca3;">← Volver al Dashboard</a>
</body>
</html>
