// Auto-extracted from tasks-manager.js (verbatim) — augments TasksManager.prototype. Loaded after the base.
Object.assign(TasksManager.prototype, {
  async refreshTasks() {
    // Show refresh notification
    const refreshNotification = window.notificationSystem.info(
      '🔄 Refreshing tasks...', 
      'Tasks', 
      null, 
      null
    );
    
    try {
      await this.loadTasks();
      
      // Remove refresh notification and show success
      if (refreshNotification && refreshNotification.remove) {
        refreshNotification.remove();
      }
      
      if (window.notificationSystem) {
        window.notificationSystem.success(
          '🔄 Tasks refreshed successfully',
          'Tasks',
          null,
          null
        );
      }
    } catch (error) {
      console.error('Error refreshing tasks:', error);
      
      // Remove refresh notification and show error
      if (refreshNotification && refreshNotification.remove) {
        refreshNotification.remove();
      }
      
      if (window.notificationSystem) {
        window.notificationSystem.error(
          `⚠️ Failed to refresh tasks: ${error.message}`,
          'Tasks',
          null,
          null
        );
      }
    }
  },
  async loadTasks() {
    try {
      //// // console.log('🔄 Loading tasks from file system...');
      
      // Check if task system is available
      if (!this.taskManager) {
        console.warn('⚠️ Task system not yet initialized, skipping task loading');
        this.tasks = [];
        return;
      }
      
      // Get tasks using new system
      
      // Get queue and current status
      let queue = [];
      let current = {};
      
      try {
        const queueResponse = await fetch('/read-file?path=tasks/queue.json');
        if (queueResponse.ok) {
          const queueText = await queueResponse.text();
          if (queueText.trim()) {  // Only parse if not empty
            try {
              queue = JSON.parse(queueText);
            } catch (parseError) {
              console.warn('⚠️ Invalid queue.json format, starting with empty queue');
              queue = [];
            }
          }
        }
      } catch (error) {
      }
      
      try {
        const currentResponse = await fetch('/read-file?path=tasks/current.json');
        if (currentResponse.ok) {
          const currentText = await currentResponse.text();
          if (currentText.trim()) {  // Only parse if not empty
            try {
              current = JSON.parse(currentText);
            } catch (parseError) {
              console.warn('⚠️ Invalid current.json format, treating as empty');
              current = {};
            }
          }
        }
      } catch (error) {
      }
      
      // Load individual task files
      const allTasks = [];
      
      // Add queued tasks
      for (const taskId of queue) {
        try {
          const task = await this.taskManager.getTask(taskId);
          if (task) allTasks.push(task);
        } catch (error) {
          console.warn(`⚠️ Failed to load queued task ${taskId}:`, error);
        }
      }
      
      // Add current task if different from queue
      if (current.id && !queue.includes(current.id)) {
        try {
          const task = await this.taskManager.getTask(current.id);
          if (task) allTasks.push(task);
        } catch (error) {
          console.warn(`⚠️ Failed to load current task ${current.id}:`, error);
        }
      }
      
      // Scan tasks folder for all task files (including completed ones) - OPTIMIZED
      try {
        const tasksResponse = await fetch('/read-directory?path=tasks');
        if (tasksResponse.ok) {
          const files = await tasksResponse.json();
          const taskFiles = files.filter(file => 
            file.endsWith('.json') && 
            file !== 'queue.json' && 
            file !== 'current.json'
          );
          
          
          // OPTIMIZATION: Batch load tasks instead of individual calls
          const missingTaskIds = taskFiles
            .map(file => file.replace('.json', ''))
            .filter(taskId => !allTasks.find(task => task.id === taskId));
          
          if (missingTaskIds.length > 0) {
            try {
              const batchResponse = await fetch('/read-tasks-batch', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ taskIds: missingTaskIds })
              });
              
              if (batchResponse.ok) {
                const batchTasks = await batchResponse.json();
                batchTasks.forEach(task => {
                  if (task) {
                    allTasks.push(task);
                  }
                });
              } else {
                // Fallback to individual loading if batch endpoint not available
                await this.loadTasksIndividually(missingTaskIds, allTasks);
              }
            } catch (error) {
              console.warn('⚠️ Batch loading failed, falling back to individual loading:', error);
              await this.loadTasksIndividually(missingTaskIds, allTasks);
            }
          }
        }
      } catch (error) {
        console.warn('⚠️ Failed to scan tasks folder:', error);
      }
      
      //// // console.log('📊 Task counts:', {
        //queued: queuedTasks.length,
        //processing: processingTasks.length,
        //completed: completedTasks.length
      //});
      
      // Combine all tasks
      this.tasks = allTasks;
      
      // Sort by creation time (newest first)
      this.tasks.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt));
      
      //// // console.log('📋 All tasks:', this.tasks);
      
      this.renderTasks();
      this.updateStats();
      this.updateSidebarCounts();
      this.generateAppCategories();
    } catch (error) {
      console.error('❌ Failed to load tasks:', error);
      if (window.notificationSystem) {
        window.notificationSystem.error(`Failed to load tasks: ${error.message}`);
      }
      this.tasks = [];
      this.renderTasks();
      this.updateStats();
      this.updateSidebarCounts();
      this.generateAppCategories();
    }
  },
  async loadTasksIndividually(taskIds, allTasks) {
    // Fallback method for individual task loading
    for (const taskId of taskIds) {
      try {
        const task = await this.taskManager.getTask(taskId);
        if (task) {
          allTasks.push(task);
        }
      } catch (error) {
        console.warn(`⚠️ Failed to load task ${taskId}:`, error);
      }
    }
  },
});
