/**
 * ProgressTracker - Manages progress state and persistence
 * Tracks completed items and calculates progress
 */

import ContentProgressAnalyzer from './ContentProgressAnalyzer.js';

class ProgressTracker {
    constructor(bookId, chapterId) {
        this.bookId = bookId;
        this.chapterId = chapterId;
        this.analyzer = new ContentProgressAnalyzer();
        this.completedItems = new Set();
        this.analysis = null;
    }

    /**
     * Initialize tracker with chapter content
     * @param {Object} chapterContent - Chapter content data
     */
    async init(chapterContent) {
        console.log(`📊 Initializing ProgressTracker for ${this.bookId}/${this.chapterId}...`);

        // Analyze chapter content
        this.analysis = this.analyzer.analyzeChapter(chapterContent, this.chapterId);

        // Load saved progress
        await this._loadProgress();

        console.log(`✅ ProgressTracker initialized: ${this.completedItems.size}/${this.analysis.items.length} items completed`);
    }

    /**
     * Load progress from server
     * @private
     */
    async _loadProgress() {
        try {
            const { default: apiService } = await import('../../services/APIService.js');
            const progress = await apiService.loadDRSProgress(this.bookId, this.chapterId);

            if (progress.completedItems && Array.isArray(progress.completedItems)) {
                progress.completedItems.forEach(itemId => {
                    this.completedItems.add(itemId);
                });
            }

            console.log(`📥 Loaded ${this.completedItems.size} completed items from server`);
        } catch (error) {
            console.log(`📥 No saved progress found (starting fresh): ${error.message}`);
        }
    }

    /**
     * Mark an item as completed
     * @param {string} itemId - Item ID to mark as completed
     * @param {Object} metadata - Optional metadata
     * @returns {Object} - Updated progress
     */
    async markCompleted(itemId, metadata = {}) {
        const item = this.analysis.items.find(i => i.id === itemId);

        if (!item) {
            throw new Error(`Unknown progress item: ${itemId}`);
        }

        // Mark item as completed
        item.markCompleted();
        this.completedItems.add(itemId);

        console.log(`✅ Marked completed: ${itemId} (${item.type}, ${item.getWeight()} points)`);

        // Save progress
        await this._saveProgress();

        // Return updated progress
        return this.getProgress();
    }

    /**
     * Get current progress
     * @returns {Object} - Progress data with percentage and breakdown
     */
    getProgress() {
        const completed = Array.from(this.completedItems);
        const progressData = this.analyzer.calculateProgress(this.chapterId, completed);

        return {
            ...progressData,
            chapterId: this.chapterId,
            bookId: this.bookId,
            completedItems: completed,
            totalItems: this.analysis.items.length
        };
    }

    /**
     * Check if an item can be completed (prerequisites met)
     * @param {string} itemId - Item ID to check
     * @param {Object} userProgress - Current user progress state
     * @returns {boolean} - True if can be completed
     */
    canComplete(itemId, userProgress) {
        const item = this.analysis.items.find(i => i.id === itemId);

        if (!item) {
            return false;
        }

        return item.canComplete(userProgress);
    }

    /**
     * Get items by type
     * @param {string} type - Item type
     * @returns {Array} - Items of specified type
     */
    getItemsByType(type) {
        return this.analysis.items.filter(item => item.type === type);
    }

    /**
     * Get uncompleted items by type
     * @param {string} type - Item type
     * @returns {Array} - Uncompleted items of specified type
     */
    getUncompletedItemsByType(type) {
        return this.analysis.items.filter(item =>
            item.type === type && !this.completedItems.has(item.id)
        );
    }

    /**
     * Save progress to server
     * @private
     */
    async _saveProgress() {
        try {
            const { default: apiService } = await import('../../services/APIService.js');

            const completedItemsData = Array.from(this.completedItems).map(itemId => {
                const item = this.analysis.items.find(i => i.id === itemId);
                return {
                    id: itemId,
                    type: item.type,
                    completedAt: item.completedAt || new Date().toISOString()
                };
            });

            await apiService.saveProgress({
                system: 'drs-progress',
                bookId: this.bookId,
                chapterId: this.chapterId,
                progressData: {
                    completedItems: completedItemsData,
                    totalWeight: this.analysis.totalWeight,
                    lastUpdated: new Date().toISOString()
                }
            });

            console.log(`💾 Progress saved: ${this.completedItems.size} items`);
        } catch (error) {
            console.error(`❌ Failed to save progress:`, error);
        }
    }

    /**
     * Reset all progress
     */
    async reset() {
        this.completedItems.clear();
        this.analysis.items.forEach(item => item.completedAt = null);
        await this._saveProgress();
        console.log(`🔄 Progress reset for ${this.bookId}/${this.chapterId}`);
    }
}

export default ProgressTracker;
