<?php
// create_admin_simple.php
session_start();

// Koneksi database
try {
    $host = 'localhost';
    $dbname = 'soca_osce';
    $username = 'root';
    $password = '';
    
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Koneksi database gagal: " . $e->getMessage());
}

// Buat admin user jika belum ada
$check_admin = $pdo->query("SELECT * FROM users WHERE role = 'admin'")->fetch();

if (!$check_admin) {
    $hashed_password = password_hash('admin123', PASSWORD_DEFAULT);
    $stmt = $pdo->prepare("INSERT INTO users (username, password, role) VALUES (?, ?, 'admin')");
    $stmt->execute(['admin', $hashed_password]);
    echo "<h2>✅ Admin user created!</h2>";
    echo "Username: <strong>admin</strong><br>";
    echo "Password: <strong>admin123</strong><br>";
} else {
    echo "<h2>✅ Admin user already exists</h2>";
    echo "Username: <strong>{$check_admin['username']}</strong><br>";
    echo "Role: <strong>{$check_admin['role']}</strong><br>";
}

// Login langsung sebagai admin
$_SESSION['user_id'] = $check_admin ? $check_admin['id'] : $pdo->lastInsertId();
$_SESSION['user_name'] = 'admin';
$_SESSION['role'] = 'admin';

echo "<h3>🎉 Anda sekarang login sebagai Admin!</h3>";
echo "<p>Session role: " . $_SESSION['role'] . "</p>";

// Redirect ke sync page
echo "<script>
    setTimeout(function() {
        window.location.href = 'admin/sync_penguji.php';
    }, 2000);
</script>";

echo "<p>Redirecting to sync page... <a href='admin/sync_penguji.php'>Click here if not redirected</a></p>";
?>