/**
 * Module Validation Test
 * Test if the Open Analysis Modules can be loaded and initialized properly
 */

import path from 'path';
import { fileURLToPath } from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

async function testModuleLoading() {
    console.log('🧪 Testing Open Analysis Modules...\n');

    const testResults = {
        textAnalysis: false,
        grammarAnalysis: false,
        translation: false,
        interfaceCompliance: false
    };

    try {
        // Test 1: Load ExerciseModuleInterface
        console.log('1️⃣ Testing ExerciseModuleInterface...');
        const { default: ExerciseModuleInterface } = await import('./src/DRS/interfaces/ExerciseModuleInterface.js');
        console.log('✅ ExerciseModuleInterface loaded successfully');

        // Test 2: Load TextAnalysisModule
        console.log('\n2️⃣ Testing TextAnalysisModule...');
        const { default: TextAnalysisModule } = await import('./src/DRS/exercise-modules/TextAnalysisModule.js');

        // Create mock dependencies
        const mockDependencies = createMockDependencies();

        // Try to instantiate
        const textModule = new TextAnalysisModule(
            mockDependencies.orchestrator,
            mockDependencies.llmValidator,
            mockDependencies.prerequisiteEngine,
            mockDependencies.contextMemory
        );

        // Test interface compliance
        const requiredMethods = ['canRun', 'present', 'validate', 'getProgress', 'cleanup', 'getMetadata'];
        const missingMethods = requiredMethods.filter(method => typeof textModule[method] !== 'function');

        if (missingMethods.length === 0) {
            console.log('✅ TextAnalysisModule implements all required methods');
            testResults.textAnalysis = true;
        } else {
            console.log('❌ TextAnalysisModule missing methods:', missingMethods);
        }

        // Test metadata
        const metadata = textModule.getMetadata();
        console.log('📋 TextAnalysisModule metadata:', metadata.name, metadata.version);

        // Test 3: Load GrammarAnalysisModule
        console.log('\n3️⃣ Testing GrammarAnalysisModule...');
        const { default: GrammarAnalysisModule } = await import('./src/DRS/exercise-modules/GrammarAnalysisModule.js');

        const grammarModule = new GrammarAnalysisModule(
            mockDependencies.orchestrator,
            mockDependencies.llmValidator,
            mockDependencies.prerequisiteEngine,
            mockDependencies.contextMemory
        );

        const grammarMissingMethods = requiredMethods.filter(method => typeof grammarModule[method] !== 'function');

        if (grammarMissingMethods.length === 0) {
            console.log('✅ GrammarAnalysisModule implements all required methods');
            testResults.grammarAnalysis = true;
        } else {
            console.log('❌ GrammarAnalysisModule missing methods:', grammarMissingMethods);
        }

        const grammarMetadata = grammarModule.getMetadata();
        console.log('📋 GrammarAnalysisModule metadata:', grammarMetadata.name, grammarMetadata.version);

        // Test 4: Load TranslationModule
        console.log('\n4️⃣ Testing TranslationModule...');
        const { default: TranslationModule } = await import('./src/DRS/exercise-modules/TranslationModule.js');

        const translationModule = new TranslationModule(
            mockDependencies.orchestrator,
            mockDependencies.llmValidator,
            mockDependencies.prerequisiteEngine,
            mockDependencies.contextMemory
        );

        const translationMissingMethods = requiredMethods.filter(method => typeof translationModule[method] !== 'function');

        if (translationMissingMethods.length === 0) {
            console.log('✅ TranslationModule implements all required methods');
            testResults.translation = true;
        } else {
            console.log('❌ TranslationModule missing methods:', translationMissingMethods);
        }

        const translationMetadata = translationModule.getMetadata();
        console.log('📋 TranslationModule metadata:', translationMetadata.name, translationMetadata.version);

        // Test 5: Interface compliance
        console.log('\n5️⃣ Testing interface compliance...');
        const allModules = [textModule, grammarModule, translationModule];
        const interfaceCompliant = allModules.every(module => module instanceof ExerciseModuleInterface);

        if (interfaceCompliant) {
            console.log('✅ All modules extend ExerciseModuleInterface properly');
            testResults.interfaceCompliance = true;
        } else {
            console.log('❌ Some modules do not properly extend ExerciseModuleInterface');
        }

        // Test 6: CanRun validation
        console.log('\n6️⃣ Testing canRun validation...');
        const mockChapterContent = {
            texts: ['Sample text for analysis'],
            grammar: ['Sample grammar exercise'],
            translations: ['Bonjour', 'Hello']
        };

        const canRunResults = allModules.map(module => {
            try {
                const canRun = module.canRun([], mockChapterContent);
                return { module: module.constructor.name, canRun, error: null };
            } catch (error) {
                return { module: module.constructor.name, canRun: false, error: error.message };
            }
        });

        canRunResults.forEach(result => {
            if (result.error) {
                console.log(`❌ ${result.module} canRun error:`, result.error);
            } else {
                console.log(`${result.canRun ? '✅' : '⚠️'} ${result.module} canRun:`, result.canRun);
            }
        });

    } catch (error) {
        console.error('❌ Critical error during testing:', error);
        console.error('Stack trace:', error.stack);
    }

    // Summary
    console.log('\n📊 Test Summary:');
    console.log('================');
    Object.entries(testResults).forEach(([test, passed]) => {
        console.log(`${passed ? '✅' : '❌'} ${test}: ${passed ? 'PASSED' : 'FAILED'}`);
    });

    const allPassed = Object.values(testResults).every(result => result);
    console.log(`\n${allPassed ? '🎉' : '⚠️'} Overall: ${allPassed ? 'ALL TESTS PASSED' : 'SOME TESTS FAILED'}`);

    return testResults;
}

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) => ({
                success: true,
                score: 0.8,
                feedback: "Mock feedback for text comprehension",
                suggestions: ["Mock suggestion 1", "Mock suggestion 2"]
            }),
            validateGrammar: async (response, context) => ({
                success: true,
                score: 0.75,
                feedback: "Mock feedback for grammar",
                explanation: "Mock grammar explanation",
                suggestions: ["Mock grammar suggestion"]
            }),
            validateTranslation: async (original, translation, context) => ({
                success: true,
                score: 0.85,
                feedback: "Mock feedback for translation",
                fluencyScore: 0.8,
                culturalContext: "Mock cultural context",
                suggestions: ["Mock translation suggestion"]
            })
        },
        prerequisiteEngine: {
            checkPrerequisites: () => true
        },
        contextMemory: {
            store: () => {},
            retrieve: () => null
        }
    };
}

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