<?php
// api/forge_key.php - 萬能鑰匙鍛造爐 (伏羲加密版)
header("Content-Type: text/html; charset=utf-8");
require_once 'db.php';

$username = 'rainzoowon';
$email = 'rainzoowon@gmail.com'; 
$password = 'zoo2026';

// 🐘 關鍵升級：將密碼進行標準的 bcrypt 加密，避開資料庫長度填充問題
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

$coins = 999999;

try {
    // 清除舊鑰匙
    $pdo->prepare("DELETE FROM users WHERE username = ? OR email = ?")->execute([$username, $email]);

    // 寫入高強度加密鑰匙
    $sql = "INSERT INTO users (username, email, password_hash, coins, level, status) 
            VALUES (?, ?, ?, ?, 99, 'active')";
    $pdo->prepare($sql)->execute([$username, $email, $hashed_password, $coins]);

    echo "<div style='background:#121212; color:#00ff88; padding:30px; font-family:sans-serif; border-radius:10px; border:1px solid #00ff88; max-width:400px; margin:50px auto;'>";
    echo "<h2 style='margin-top:0;'>✅ 萬能鑰匙鍛造成功 (加密版)！</h2>";
    echo "<p style='color:#ccc;'>統帥，資料庫已寫入高強度加密鑰匙：</p>";
    echo "<ul style='color:#fff; font-size:18px;'>";
    echo "<li>登入信箱：<b>{$email}</b></li>";
    echo "<li>登入密碼：<b>{$password}</b></li>";
    echo "</ul>";
    echo "<p style='color:#00ced1; font-weight:bold;'>加密鎖已就位，請安心登入。</p>";
    echo "</div>";

} catch (Exception $e) {
    echo "<h2 style='color:red;'>❌ 鍛造失敗</h2><p>" . $e->getMessage() . "</p>";
}
?>