<?php
// Safe script to add reset_token and reset_expiry columns if they don't exist.
error_reporting(E_ALL);
ini_set('display_errors', 1);

require_once __DIR__ . '/db_config.php';

$conn = $conn;  // Already set by db_config.php
if ($conn->connect_error) {
    echo "DB connection failed: " . $conn->connect_error;
    exit;
}
$conn->set_charset('utf8mb4');

echo "Checking table structure...\n\n";

// Get current fields
$res = $conn->query("DESCRIBE `login-t`");
$fields = [];
while ($row = $res->fetch_assoc()) {
    $fields[] = $row['Field'];
}

// Add missing columns
$added = [];
if (!in_array('reset_token', $fields)) {
    $conn->query("ALTER TABLE `login-t` ADD COLUMN `reset_token` VARCHAR(255) NULL DEFAULT NULL");
    $added[] = 'reset_token';
}
if (!in_array('reset_expiry', $fields)) {
    $conn->query("ALTER TABLE `login-t` ADD COLUMN `reset_expiry` DATETIME NULL DEFAULT NULL");
    $added[] = 'reset_expiry';
}

if (empty($added)) {
    echo "✓ All required columns already exist!\n";
} else {
    echo "✓ Added columns: " . implode(', ', $added) . "\n";
}

// show final table structure
echo "\nFinal table structure:\n";
$res = $conn->query("DESCRIBE `login-t`");
while ($row = $res->fetch_assoc()) {
    echo "  - " . $row['Field'] . " (" . $row['Type'] . ")\n";
}

$conn->close();
?>
