<?php
// Migration helper: adds has_shop_button and shop_link columns to `news` if missing.
// Usage: run this script once (via browser or CLI) and then delete it.

require_once('db_config.php');
if (!$conn) {
    die('Database connection not available.');
}

function columnExists($conn, $table, $column) {
    $tbl = $conn->real_escape_string($table);
    $col = $conn->real_escape_string($column);
    $res = $conn->query("SHOW COLUMNS FROM `".$tbl."` LIKE '".$col."'");
    return ($res && $res->num_rows > 0);
}

$added = [];
if (!columnExists($conn, 'news', 'has_shop_button')) {
    $q = "ALTER TABLE news ADD COLUMN has_shop_button TINYINT(1) NOT NULL DEFAULT 0";
    if ($conn->query($q)) { $added[] = 'has_shop_button'; } else { $err1 = $conn->error; }
}
if (!columnExists($conn, 'news', 'shop_link')) {
    $q = "ALTER TABLE news ADD COLUMN shop_link VARCHAR(255) DEFAULT NULL";
    if ($conn->query($q)) { $added[] = 'shop_link'; } else { $err2 = $conn->error; }
}

?><!doctype html>
<html>
<head><meta charset="utf-8"><title>Migration Result</title></head>
<body>
<h2>Migration run</h2>
<?php if (!empty($added)): ?>
    <p>Added columns: <?php echo htmlspecialchars(implode(', ', $added)); ?></p>
<?php else: ?>
    <p>No new columns were added.</p>
<?php endif; ?>
<?php if (!empty($err1) || !empty($err2)): ?>
    <h3>Errors</h3>
    <pre><?php echo htmlspecialchars(($err1 ?? '') . "\n" . ($err2 ?? '')); ?></pre>
<?php endif; ?>
<p>Delete this file after successful migration.</p>
</body>
</html>