<?php
// api/super_fix_v2.php - 暴力擴建所有可能遺失的欄位
header("Content-Type: text/html; charset=utf-8");
require_once 'db.php';

echo "<body style='background:#0a0a0a; color:#00ff88; font-family:sans-serif; padding:20px; text-align:center;'>";
echo "<h1>🔨 園區資料庫終極暴力擴建中...</h1>";

$columns_to_add = [
    'quantity' => 'INT NOT NULL DEFAULT 10',
    'reward' => 'INT NOT NULL DEFAULT 20',
    'budget' => 'INT NOT NULL DEFAULT 0',
    'duration' => 'INT NOT NULL DEFAULT 15',
    'require_screenshot' => 'TINYINT(1) NOT NULL DEFAULT 0'
];

foreach ($columns_to_add as $col_name => $col_def) {
    try {
        // 暴力加入，不指定 AFTER 順序，直接塞到最後
        $sql = "ALTER TABLE tasks ADD COLUMN `$col_name` $col_def";
        $pdo->exec($sql);
        echo "<p>✅ 成功擴建：`$col_name` 欄位</p>";
    } catch (PDOException $e) {
        // 錯誤碼 42S21 代表 "Duplicate column name" (欄位已存在)
        if ($e->getCode() == '42S21') {
            echo "<p style='color:#888;'>👌 `$col_name` 欄位已存在，跳過</p>";
        } else {
            // 其他錯誤才顯示紅色警告
            echo "<p style='color:red;'>❌ 擴建 `$col_name` 失敗：" . $e->getMessage() . "</p>";
        }
    }
}

echo "<h2 style='color:#ffd700; margin-top:30px;'>🎉 終極擴建完成！所有欄位皆已就位！</h2>";
echo "<a href='../projects.html' style='display:inline-block; margin-top:20px; padding:15px 30px; background:#ffd700; color:#000; text-decoration:none; font-weight:bold; border-radius:50px;'>🍎 再次嘗試發布餵食</a>";
echo "</body>";
?>