<?php
// api/setup_exchange.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 exchange_requests (
        id INT AUTO_INCREMENT PRIMARY KEY,
        user_id INT NOT NULL,
        item_name VARCHAR(100) NOT NULL,
        item_type VARCHAR(20) NOT NULL COMMENT 'virtual 或 physical',
        cost INT NOT NULL,
        contact_info TEXT NOT NULL COMMENT '聯絡信箱或收件地址',
        status VARCHAR(20) DEFAULT 'pending' COMMENT 'pending(待處理), approved(已發貨), rejected(已退回)',
        admin_note TEXT COMMENT '存放發送的虛擬序號或物流單號',
        created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
        updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;";

    $pdo->exec($sql);
    echo "<p>✅ 成功擴建：`exchange_requests` (禮品兌換訂單表) 已就緒！</p>";
    echo "<h3 style='color:#ffd700;'>🎉 倉庫建立完成！您可以關閉此頁面了。</h3>";

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