<?php
require_once 'db.php';

try {
    // 1. Ubah enum role di tabel users agar mengizinkan 'pimpinan'
    $pdo->exec("ALTER TABLE `users` MODIFY COLUMN `role` ENUM('superadmin', 'admin_instansi', 'pimpinan') NOT NULL DEFAULT 'admin_instansi'");
    
    // 2. Cek apakah user 'pimpinan' sudah ada
    $stmt = $pdo->prepare("SELECT id FROM users WHERE username = 'pimpinan'");
    $stmt->execute();
    if ($stmt->rowCount() == 0) {
        $pass_hash = password_hash('pimpinan123', PASSWORD_BCRYPT);
        // Anggap instansi_id = 1
        $pdo->exec("INSERT INTO users (username, password, role, instansi_id) VALUES ('pimpinan', '$pass_hash', 'pimpinan', 1)");
        echo "Berhasil! Akun pimpinan berhasil dibuat.\n";
    } else {
        // Update password just in case
        $pass_hash = password_hash('pimpinan123', PASSWORD_BCRYPT);
        $pdo->exec("UPDATE users SET password = '$pass_hash', role = 'pimpinan' WHERE username = 'pimpinan'");
        echo "Akun pimpinan sudah ada dan password telah direset.\n";
    }
    
    echo "Gunakan Username: pimpinan\n";
    echo "Gunakan Password: pimpinan123\n";
    
} catch (PDOException $e) {
    echo "Error: " . $e->getMessage();
}
