<?php
require_once __DIR__ . '/config.php';
$db = getDB();
$results = [];

// Verificar columnas existentes
$existing = [];
$rows = $db->query("DESCRIBE transacciones")->fetchAll();
foreach ($rows as $r) $existing[] = $r['Field'];

$cols = [
    'skydropx_shipment_id' => "ALTER TABLE transacciones ADD COLUMN skydropx_shipment_id VARCHAR(64) NULL DEFAULT NULL",
    'tracking_number'      => "ALTER TABLE transacciones ADD COLUMN tracking_number VARCHAR(64) NULL DEFAULT NULL",
    'label_url'            => "ALTER TABLE transacciones ADD COLUMN label_url VARCHAR(512) NULL DEFAULT NULL",
    'carrier'              => "ALTER TABLE transacciones ADD COLUMN carrier VARCHAR(64) NULL DEFAULT NULL",
];

foreach ($cols as $col => $sql) {
    if (in_array($col, $existing)) {
        $results[] = ['skip' => "$col ya existe"];
        continue;
    }
    try {
        $db->exec($sql);
        $results[] = ['ok' => "Columna $col agregada"];
    } catch (Exception $e) {
        $results[] = ['err' => $e->getMessage()];
    }
}

header('Content-Type: application/json');
echo json_encode(['results' => $results]);
