/**
 * Dynamic System Manager
 * Manages dynamic messaging, progress indicators, and system status updates
 */

class DynamicSystemManager {
    constructor(subscriptionManager = null) {
        this.subscriptionManager = subscriptionManager;
        this.activeProcesses = new Map();
        this.messageQueue = [];
        this.isProcessing = false;
    }

    /**
     * Start a dynamic processing indicator
     */
    startProcess(processId, options = {}) {
        const process = {
            id: processId,
            startTime: Date.now(),
            status: 'active',
            progress: 0,
            message: options.message || 'Processing...',
            ...options
        };
        this.activeProcesses.set(processId, process);
        return process;
    }

    /**
     * Update process progress
     */
    updateProgress(processId, progress, message = null) {
        const process = this.activeProcesses.get(processId);
        if (process) {
            process.progress = Math.min(100, Math.max(0, progress));
            if (message) process.message = message;
            process.lastUpdate = Date.now();
        }
        return process;
    }

    /**
     * Complete a process
     */
    completeProcess(processId, result = null) {
        const process = this.activeProcesses.get(processId);
        if (process) {
            process.status = 'completed';
            process.progress = 100;
            process.endTime = Date.now();
            process.duration = process.endTime - process.startTime;
            process.result = result;
            
            // Auto-cleanup after 5 seconds
            setTimeout(() => {
                this.activeProcesses.delete(processId);
            }, 5000);
        }
        return process;
    }

    /**
     * Fail a process
     */
    failProcess(processId, error) {
        const process = this.activeProcesses.get(processId);
        if (process) {
            process.status = 'failed';
            process.endTime = Date.now();
            process.error = error.message || error;
        }
        return process;
    }

    /**
     * Get all active processes
     */
    getActiveProcesses() {
        return Array.from(this.activeProcesses.values())
            .filter(p => p.status === 'active');
    }

    /**
     * Queue a message for processing
     */
    queueMessage(message) {
        this.messageQueue.push({
            ...message,
            queuedAt: Date.now()
        });
        this.processQueue();
    }

    /**
     * Process queued messages
     */
    async processQueue() {
        if (this.isProcessing || this.messageQueue.length === 0) return;
        
        this.isProcessing = true;
        while (this.messageQueue.length > 0) {
            const message = this.messageQueue.shift();
            try {
                await this.handleMessage(message);
            } catch (error) {
                console.error('Error processing queued message:', error);
            }
        }
        this.isProcessing = false;
    }

    /**
     * Handle a single message
     */
    async handleMessage(message) {
        // Override in subclass or configure handler
        console.log('Processing message:', message.type);
    }

    /**
     * Get system status
     */
    getSystemStatus() {
        return {
            activeProcesses: this.activeProcesses.size,
            queuedMessages: this.messageQueue.length,
            isProcessing: this.isProcessing,
            uptime: process.uptime ? process.uptime() : 0
        };
    }
}

module.exports = { DynamicSystemManager };
