/**
 * ImplementationValidator - Validates all progress item implementations at startup
 * Ensures ALL required methods are implemented before app runs
 */

import VocabularyDiscoveryItem from '../progress-items/VocabularyDiscoveryItem.js';
import VocabularyMasteryItem from '../progress-items/VocabularyMasteryItem.js';
import {
    PhraseItem,
    DialogItem,
    TextItem,
    AudioItem,
    ImageItem,
    GrammarItem
} from '../progress-items/ContentProgressItems.js';

class ImplementationValidator {
    static async validateAll() {
        console.log('%c🔍 VALIDATING PROGRESS ITEM IMPLEMENTATIONS...',
            'background: #3b82f6; color: white; font-size: 16px; padding: 8px; font-weight: bold;'
        );

        const errors = [];
        const validations = [];

        // Test VocabularyDiscoveryItem
        validations.push(
            this._testItem('VocabularyDiscoveryItem', () =>
                new VocabularyDiscoveryItem('test', { user_language: 'test' })
            )
        );

        // Test VocabularyMasteryItem
        validations.push(
            this._testItem('VocabularyMasteryItem', () =>
                new VocabularyMasteryItem('test', { user_language: 'test' })
            )
        );

        // Test PhraseItem
        validations.push(
            this._testItem('PhraseItem', () =>
                new PhraseItem('test', { text: 'test' })
            )
        );

        // Test DialogItem
        validations.push(
            this._testItem('DialogItem', () =>
                new DialogItem('test', { title: 'test' })
            )
        );

        // Test TextItem
        validations.push(
            this._testItem('TextItem', () =>
                new TextItem('test', { content: 'test' })
            )
        );

        // Test AudioItem
        validations.push(
            this._testItem('AudioItem', () =>
                new AudioItem('test', { url: 'test' })
            )
        );

        // Test ImageItem
        validations.push(
            this._testItem('ImageItem', () =>
                new ImageItem(0, { url: 'test' })
            )
        );

        // Test GrammarItem
        validations.push(
            this._testItem('GrammarItem', () =>
                new GrammarItem(0, { rule: 'test' })
            )
        );

        // Wait for all validations
        const results = await Promise.allSettled(validations);

        results.forEach((result, index) => {
            if (result.status === 'rejected') {
                errors.push(result.reason);
            }
        });

        if (errors.length > 0) {
            console.error('%c❌ VALIDATION FAILED',
                'background: red; color: white; font-size: 20px; padding: 10px; font-weight: bold;'
            );
            errors.forEach(e => console.error(e));
            return false;
        }

        console.log('%c✅ ALL IMPLEMENTATIONS VALID',
            'background: #10b981; color: white; font-size: 16px; padding: 8px; font-weight: bold;'
        );
        return true;
    }

    static async _testItem(className, createFn) {
        try {
            const item = createFn();

            // Test all required methods
            const requiredMethods = ['validate', 'serialize', 'getWeight', 'canComplete'];
            const mockUserProgress = { discoveredWords: [], masteredWords: [] };

            requiredMethods.forEach(method => {
                if (typeof item[method] !== 'function') {
                    throw new Error(`${className}: Missing method ${method}()`);
                }
            });

            // Actually call the methods to ensure they don't throw
            item.validate();
            item.serialize();
            item.getWeight();
            item.canComplete(mockUserProgress);

            console.log(`✅ ${className} - OK`);
            return true;
        } catch (error) {
            console.error(`❌ ${className} - FAILED:`, error.message);
            throw error;
        }
    }
}

export default ImplementationValidator;
