/* ----------------------------------------------------
   FILE: Tutorial_Logic.js
   ---------------------------------------------------- */

// Kita buat fungsi agar bisa dipanggil manual jika perlu
function checkAndShowTutorial() {
    // 1. Ambil user terbaru saat fungsi dijalankan
    const currentUser = sessionStorage.getItem("loggedInUser") || "guest";
    const tutorialKey = 'tutorialSeen_' + currentUser;

    // DEBUG: Cek di Console browser (tekan F12 -> Console)
    console.log(`[Tutorial Check] User: ${currentUser}`);
    console.log(`[Tutorial Check] Key: ${tutorialKey}`);
    
    // 2. Cek status di LocalStorage
    const hasSeenTutorial = localStorage.getItem(tutorialKey);
    console.log(`[Tutorial Check] Status Seen: ${hasSeenTutorial}`);

    const tutorialOverlay = document.getElementById('tutorial-overlay');

    // 3. Logic: Jika belum pernah lihat (null) -> Tampilkan
    if (!hasSeenTutorial && tutorialOverlay) {
        console.log("-> Menampilkan Tutorial untuk user baru.");
        tutorialOverlay.style.display = 'flex';
    } else {
        console.log("-> User ini sudah pernah lihat tutorial (atau overlay tidak ketemu).");
    }
}

// Jalankan otomatis saat halaman selesai loading
document.addEventListener('DOMContentLoaded', () => {
    checkAndShowTutorial();

    // Setup tombol close hanya sekali
    const closeTutorialBtn = document.getElementById('close-tutorial');
    const tutorialOverlay = document.getElementById('tutorial-overlay');
    
    if (closeTutorialBtn) {
        closeTutorialBtn.addEventListener('click', () => {
            // Ambil user SAAT INI (penting jika user berubah tanpa reload)
            const currentUser = sessionStorage.getItem("loggedInUser") || "guest";
            const tutorialKey = 'tutorialSeen_' + currentUser;
            
            if(tutorialOverlay) tutorialOverlay.style.display = 'none';
            localStorage.setItem(tutorialKey, 'true');
            console.log(`[Tutorial Check] Disimpan: ${tutorialKey} = true`);
        });
    }
});

// (Opsional) Bikin global biar bisa dipanggil dari file lain/console
window.checkTutorial = checkAndShowTutorial;