<?php
// api/setup_gifts.php - 建立動態禮品資料庫
header("Content-Type: text/html; charset=utf-8");
require_once 'db.php';

echo "<body style='background:#0a0a0a; color:#00ff88; padding:30px; font-family:sans-serif;'>";
echo "<h2>🛍️ 禮品動態倉庫擴建中...</h2>";

try {
    $sql = "CREATE TABLE IF NOT EXISTS gifts (
        id INT AUTO_INCREMENT PRIMARY KEY,
        name VARCHAR(100) NOT NULL COMMENT '禮品名稱',
        type VARCHAR(20) NOT NULL COMMENT 'virtual 或 physical',
        cost INT NOT NULL COMMENT 'ZOO幣價格',
        icon VARCHAR(20) DEFAULT '🎁' COMMENT 'Emoji圖示',
        description TEXT COMMENT '禮品說明',
        is_active TINYINT(1) DEFAULT 1 COMMENT '1=上架中, 0=已下架',
        created_at DATETIME DEFAULT CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";

    $pdo->exec($sql);
    echo "<p>✅ 成功擴建：`gifts` (動態禮品資料表) 已就緒！</p>";

    // 預先塞入三個預設商品，讓您一開始就有東西可以看
    $check = $pdo->query("SELECT COUNT(*) FROM gifts")->fetchColumn();
    if ($check == 0) {
        $pdo->exec("INSERT INTO gifts (name, type, cost, icon, description) VALUES 
            ('超商咖啡兌換序號', 'virtual', 5000, '☕', '憑序號至各大超商兌換中杯拿鐵一杯。'),
            ('LINE 貼圖點數 (50點)', 'virtual', 8000, '💬', '發放一組序號，可儲值至您的 LINE 帳號。'),
            ('園區特製吉祥物抱枕', 'physical', 25000, '🧸', '超可愛的 Zoo 兔實體抱枕，免運費宅配到府！')");
        echo "<p>🎁 已為您將 3 項基礎商品放入倉庫中！</p>";
    }

    echo "<h3 style='color:#ffd700;'>🎉 禮品倉庫建立完成！您可以關閉此頁面了。</h3>";

} catch (PDOException $e) {
    echo "<p style='color:red;'>❌ 擴建失敗：" . $e->getMessage() . "</p>";
}
echo "</body>";
?>