const memory = require('./memory.js'); // Scheduler memory

module.exports = async function() {
    try {
        const allTasks = memory.getAllTasks();

        // Build the return object with task status
        const tasksStatus = {};
        for (const name in allTasks) {
            const t = allTasks[name];
            tasksStatus[name] = {
                active: t.active,            // whether the task is currently marked active
                oneTime: t.oneTime,          // one-time or recurring
                intervalInSec: t.intervalInSec,
                lastRun: t.lastRun,          // last execution timestamp
                nextRun: t.nextRun,          // next scheduled execution timestamp
                error: t.error,              // last error if occurred
                action: t.action             // linked action file
            };
        }

        // Determine if service is online (any task has a timer running)
        const online = Object.values(allTasks).some(t => t.timer);

        return {
            online: online,
            tasks: tasksStatus
        };
    } catch (err) {
        console.error('Failed to retrieve scheduler status:', err);
        return {
            online: false,
            tasks: {}
        };
    }
};