/**
 * Real AI Integration Test
 * Test Open Analysis Modules with actual AI responses
 */

function createMockDependencies() {
    return {
        orchestrator: {
            getSharedServices: () => ({
                iaEngine: {
                    validateEducationalContent: async (prompt, options) => ({
                        content: JSON.stringify([{
                            question: "Test question",
                            expectations: "Test expectations",
                            targetLength: 100
                        }])
                    })
                }
            }),
            handleExerciseCompletion: (data) => {
                console.log('📊 Exercise completion handled:', data.moduleType);
            }
        },
        llmValidator: {
            isAvailable: () => true,
            validateTextComprehension: async (text, response, context) => {
                // This will use the real IAEngine now
                const { default: LLMValidator } = await import('./src/DRS/services/LLMValidator.js');
                const validator = new LLMValidator();
                return await validator.validateTextComprehension(text, response, context);
            }
        },
        prerequisiteEngine: {
            checkPrerequisites: () => true
        },
        contextMemory: {
            store: () => {},
            retrieve: () => null
        }
    };
}

async function testRealAIIntegration() {
    console.log('🚀 Testing Open Analysis Modules with Real AI Integration...\n');

    try {
        // Test TextAnalysisModule with real AI
        console.log('1️⃣ Testing TextAnalysisModule with real AI...');
        const { default: TextAnalysisModule } = await import('./src/DRS/exercise-modules/TextAnalysisModule.js');

        const mockDeps = createMockDependencies();
        const textModule = new TextAnalysisModule(
            mockDeps.orchestrator,
            mockDeps.llmValidator,
            mockDeps.prerequisiteEngine,
            mockDeps.contextMemory
        );

        // Real content with proper structure
        const realContent = {
            text: "Climate change is one of the most pressing issues of our time. Rising global temperatures are causing ice caps to melt, sea levels to rise, and weather patterns to become more extreme.",
            title: "Climate Change",
            difficulty: "intermediate",
            language: "en"
        };

        // Test if module can run
        const canRun = textModule.canRun([], { texts: [realContent] });
        console.log('TextAnalysisModule canRun:', canRun);

        if (canRun) {
            // Test present method with proper context
            const context = {
                content: realContent,
                difficulty: "intermediate",
                language: "en"
            };
            const presentation = textModule.present(context);
            console.log('TextAnalysisModule presentation type:', typeof presentation);

            // Test validate method with AI
            console.log('Testing AI validation...');
            const userResponse = "Climate change causes global warming and extreme weather.";
            const validationContext = {
                text: realContent.text,
                difficulty: "intermediate",
                language: "en"
            };
            const validation = await textModule.validate(userResponse, validationContext);

            console.log('✅ Real AI Validation Result:');
            console.log('- Success:', validation.success);
            console.log('- Score:', validation.score);
            console.log('- Provider:', validation.provider);
            console.log('- Feedback preview:', validation.feedback?.substring(0, 150));
        }

        console.log('\n🎯 Real AI integration test completed!');

    } catch (error) {
        console.error('❌ Real AI integration test failed:', error.message);
        console.error('Stack:', error.stack);
    }
}

// Run the test
testRealAIIntegration().catch(error => {
    console.error('❌ Test execution failed:', error);
    process.exit(1);
});