/**
 * Debug Module Loading - Helps diagnose what modules are actually loaded
 */

window.debugModules = function() {
    console.log('🔍 DEBUGGING MODULE STATUS');
    console.log('=========================');

    // Check application
    console.log('📱 Application:', {
        exists: !!window.app,
        status: window.app?.getStatus?.()?.status,
        core: !!window.app?.getCore(),
        moduleLoader: !!window.app?.getCore()?.moduleLoader
    });

    // Check module loader
    if (window.app?.getCore()?.moduleLoader) {
        const moduleLoader = window.app.getCore().moduleLoader;
        console.log('📦 ModuleLoader status:', moduleLoader.getStatus());

        // List all loaded modules
        const modules = ['eventBus', 'router', 'intelligentSequencer', 'flashcardLearning',
                        'unifiedDRS', 'iaEngine', 'llmValidator', 'smartPreviewOrchestrator'];

        console.log('📋 Module availability:');
        modules.forEach(name => {
            const module = moduleLoader.getModule(name);
            console.log(`  ${name}: ${module ? '✅' : '❌'}`);
            if (module && name === 'smartPreviewOrchestrator') {
                console.log(`    - sharedServices: ${!!module.sharedServices}`);
                console.log(`    - prerequisiteEngine: ${!!module.sharedServices?.prerequisiteEngine}`);
            }
        });
    }

    // Check global variables
    console.log('🌐 Global variables:');
    const globals = ['contentLoader', 'unifiedDRS', 'iaEngine', 'llmValidator', 'prerequisiteEngine'];
    globals.forEach(name => {
        console.log(`  window.${name}: ${!!window[name]}`);
    });

    // Check if modules are properly initialized
    console.log('🚀 Module initialization:');
    if (window.app?.getCore()?.moduleLoader) {
        const orchestrator = window.app.getCore().moduleLoader.getModule('smartPreviewOrchestrator');
        if (orchestrator) {
            console.log('  SmartPreviewOrchestrator:', {
                exists: true,
                sharedServices: !!orchestrator.sharedServices,
                prerequisiteEngine: !!orchestrator.sharedServices?.prerequisiteEngine,
                contextMemory: !!orchestrator.sharedServices?.contextMemory,
                iaEngine: !!orchestrator.sharedServices?.iaEngine
            });
        } else {
            console.log('  SmartPreviewOrchestrator: ❌ Not found');
        }
    }

    // Try to access prerequisite engine directly
    console.log('🎯 PrerequisiteEngine access attempts:');

    // Method 1: From global
    console.log(`  1. window.prerequisiteEngine: ${!!window.prerequisiteEngine}`);

    // Method 2: From orchestrator
    if (window.app?.getCore()?.moduleLoader) {
        const orchestrator = window.app.getCore().moduleLoader.getModule('smartPreviewOrchestrator');
        const fromOrchestrator = orchestrator?.sharedServices?.prerequisiteEngine;
        console.log(`  2. From orchestrator: ${!!fromOrchestrator}`);

        if (fromOrchestrator) {
            console.log('     - markWordDiscovered method:', typeof fromOrchestrator.markWordDiscovered);
            console.log('     - isMastered method:', typeof fromOrchestrator.isMastered);
        }
    }

    // Method 3: Check if it exists but not global
    if (window.unifiedDRS && window.unifiedDRS._prerequisiteEngine) {
        console.log(`  3. From unifiedDRS: ${!!window.unifiedDRS._prerequisiteEngine}`);
    }
};

// Auto-run when loaded
setTimeout(() => {
    if (window.app && window.app.getStatus && window.app.getStatus().isRunning) {
        window.debugModules();
    } else {
        console.log('⏳ App not ready yet, run debugModules() manually when ready');
    }
}, 2000);

console.log('🔍 Debug modules loaded. Run debugModules() to see detailed module status.');