<?php
// migrations/2025_08_add_pin_hash.php
// Ejecuta esto en: http://localhost/miportal_pro/migrations/2025_08_add_pin_hash.php

require_once __DIR__ . '/../includes/db.php';

try {
  $pdo = pdo();
  // Verificar si la columna pin_hash ya existe
  $sql = "SELECT COUNT(*) FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'users' AND column_name = 'pin_hash'";
  $exists = (int)$pdo->query($sql)->fetchColumn();

  if ($exists === 0) {
    $pdo->exec("ALTER TABLE users ADD COLUMN pin_hash VARCHAR(255) NULL AFTER password_hash");
    echo "OK: Columna pin_hash agregada.\n";
  } else {
    echo "OK: Columna pin_hash ya existe.\n";
  }

  echo "Listo. Puedes volver a la página de Clientes.";
} catch (Throwable $e) {
  http_response_code(500);
  echo "ERROR: " . $e->getMessage();
}
