<?php
require_once 'config.php';
$pdo = getDBConnection();

if (!$pdo) {
    die("<h1>Error: Could not connect to database.</h1>");
}

try {
    $pdo->exec("ALTER TABLE mediciones 
        ADD COLUMN IF NOT EXISTS buque_id VARCHAR(50) AFTER id,
        ADD COLUMN IF NOT EXISTS temp1 DECIMAL(5,2),
        ADD COLUMN IF NOT EXISTS temp2 DECIMAL(5,2),
        ADD COLUMN IF NOT EXISTS temp3 DECIMAL(5,2),
        ADD COLUMN IF NOT EXISTS temp4 DECIMAL(5,2),
        ADD COLUMN IF NOT EXISTS temp5 DECIMAL(5,2),
        ADD COLUMN IF NOT EXISTS temp6 DECIMAL(5,2),
        ADD COLUMN IF NOT EXISTS temp7 DECIMAL(5,2),
        ADD COLUMN IF NOT EXISTS temp8 DECIMAL(5,2),
        ADD COLUMN IF NOT EXISTS temp9 DECIMAL(5,2),
        ADD COLUMN IF NOT EXISTS temp10 DECIMAL(5,2)");

    // Also update existing records to have a buque_id if missing
    $pdo->exec("UPDATE mediciones SET buque_id = 'BUQUE-01' WHERE buque_id IS NULL OR buque_id = ''");

    echo "<h1>Database Extension Successful</h1>";
    echo "<p>Columns temp1 through temp10 and buque_id are now ready.</p>";
} catch (Exception $e) {
    echo "<h1>Error updating database</h1>";
    echo "<pre>" . $e->getMessage() . "</pre>";
}
?>