<?php
require_once __DIR__ . '/../config/config.php';

$username = 'admin';
$password = 'admin123';
$hashed_password = password_hash($password, PASSWORD_DEFAULT);

try {
    // 檢查用戶是否存在
    $stmt = $pdo->prepare("SELECT id FROM admins WHERE username = ?");
    $stmt->execute([$username]);
    $user = $stmt->fetch();

    if ($user) {
        // 更新密碼
        $stmt = $pdo->prepare("UPDATE admins SET password = ? WHERE username = ?");
        $stmt->execute([$hashed_password, $username]);
        echo "<h1>密碼重置成功！</h1>";
        echo "<p>用戶名：<strong>admin</strong></p>";
        echo "<p>新密碼：<strong>admin123</strong></p>";
    } else {
        // 如果用戶不存在則創建
        $stmt = $pdo->prepare("INSERT INTO admins (username, password, role) VALUES (?, ?, 'admin')");
        $stmt->execute([$username, $hashed_password]);
        echo "<h1>帳號創建成功！</h1>";
        echo "<p>用戶名：<strong>admin</strong></p>";
        echo "<p>密碼：<strong>admin123</strong></p>";
    }
    echo "<br><a href='login.php'>前往登入頁面</a>";
} catch (PDOException $e) {
    echo "<h1>錯誤：</h1>" . $e->getMessage();
}
?>