/**
 * ContentProgressItems - All content-based progress items
 * (Phrases, Dialogs, Texts, Audio, Image, Grammar)
 */

import ProgressItemInterface from '../interfaces/ProgressItemInterface.js';

/**
 * PhraseItem - Progress item for phrase comprehension
 * Weight: 1 point
 * Prerequisites: Vocabulary must be mastered
 */
class PhraseItem extends ProgressItemInterface {
    constructor(phraseId, data) {
        super(ProgressItemInterface.TYPES.PHRASE, `phrase-${phraseId}`, { phraseId, ...data });
        this.phraseId = phraseId;
        this.data = data;
    }

    validate() {
        if (!this.phraseId) throw new Error('PhraseItem: Invalid phraseId');
        return true;
    }

    serialize() {
        return { ...this._getBaseSerialization(), phraseId: this.phraseId, data: this.data };
    }

    getWeight() {
        return ProgressItemInterface.WEIGHTS['phrase'];
    }

    canComplete(userProgress) {
        // Phrase can be completed if vocabulary is mastered
        // TODO: Check specific words in phrase
        return true;
    }
}

/**
 * DialogItem - Progress item for dialog comprehension
 * Weight: 3 points (more complex)
 * Prerequisites: Vocabulary must be mastered
 */
class DialogItem extends ProgressItemInterface {
    constructor(dialogId, data) {
        super(ProgressItemInterface.TYPES.DIALOG, `dialog-${dialogId}`, { dialogId, ...data });
        this.dialogId = dialogId;
        this.data = data;
    }

    validate() {
        if (!this.dialogId) throw new Error('DialogItem: Invalid dialogId');
        return true;
    }

    serialize() {
        return { ...this._getBaseSerialization(), dialogId: this.dialogId, data: this.data };
    }

    getWeight() {
        return ProgressItemInterface.WEIGHTS['dialog'];
    }

    canComplete(userProgress) {
        // Dialog can be completed if vocabulary is mastered
        // TODO: Check specific words in dialog
        return true;
    }
}

/**
 * TextItem - Progress item for text/lesson comprehension
 * Weight: 5 points (most complex)
 * Prerequisites: Vocabulary must be mastered
 */
class TextItem extends ProgressItemInterface {
    constructor(lessonId, data) {
        super(ProgressItemInterface.TYPES.TEXT, `lesson-${lessonId}`, { lessonId, ...data });
        this.lessonId = lessonId;
        this.data = data;
    }

    validate() {
        if (!this.lessonId) throw new Error('TextItem: Invalid lessonId');
        return true;
    }

    serialize() {
        return { ...this._getBaseSerialization(), lessonId: this.lessonId, data: this.data };
    }

    getWeight() {
        return ProgressItemInterface.WEIGHTS['text'];
    }

    canComplete(userProgress) {
        // Text can be completed if vocabulary is mastered
        // TODO: Check specific words in text
        return true;
    }
}

/**
 * AudioItem - Progress item for audio comprehension
 * Weight: 4 points
 * Prerequisites: Vocabulary must be mastered
 */
class AudioItem extends ProgressItemInterface {
    constructor(audioId, data) {
        super(ProgressItemInterface.TYPES.AUDIO, `audio-${audioId}`, { audioId, ...data });
        this.audioId = audioId;
        this.data = data;
    }

    validate() {
        if (!this.audioId) throw new Error('AudioItem: Invalid audioId');
        return true;
    }

    serialize() {
        return { ...this._getBaseSerialization(), audioId: this.audioId, data: this.data };
    }

    getWeight() {
        return ProgressItemInterface.WEIGHTS['audio'];
    }

    canComplete(userProgress) {
        // Audio can be completed if vocabulary is mastered
        return true;
    }
}

/**
 * ImageItem - Progress item for image description
 * Weight: 2 points
 * Prerequisites: Vocabulary discovered (not necessarily mastered)
 */
class ImageItem extends ProgressItemInterface {
    constructor(imageId, data) {
        super(ProgressItemInterface.TYPES.IMAGE, `image-${imageId}`, { imageId, ...data });
        this.imageId = imageId;
        this.data = data;
    }

    validate() {
        if (this.imageId === undefined) throw new Error('ImageItem: Invalid imageId');
        return true;
    }

    serialize() {
        return { ...this._getBaseSerialization(), imageId: this.imageId, data: this.data };
    }

    getWeight() {
        return ProgressItemInterface.WEIGHTS['image'];
    }

    canComplete(userProgress) {
        // Image can be completed if vocabulary is discovered
        return true;
    }
}

/**
 * GrammarItem - Progress item for grammar rules
 * Weight: 2 points
 * Prerequisites: Vocabulary discovered (not necessarily mastered)
 */
class GrammarItem extends ProgressItemInterface {
    constructor(grammarId, data) {
        super(ProgressItemInterface.TYPES.GRAMMAR, `grammar-${grammarId}`, { grammarId, ...data });
        this.grammarId = grammarId;
        this.data = data;
    }

    validate() {
        if (this.grammarId === undefined) throw new Error('GrammarItem: Invalid grammarId');
        return true;
    }

    serialize() {
        return { ...this._getBaseSerialization(), grammarId: this.grammarId, data: this.data };
    }

    getWeight() {
        return ProgressItemInterface.WEIGHTS['grammar'];
    }

    canComplete(userProgress) {
        // Grammar can be completed if vocabulary is discovered
        return true;
    }
}

export {
    PhraseItem,
    DialogItem,
    TextItem,
    AudioItem,
    ImageItem,
    GrammarItem
};
