/*
 * Copyright 2026, Jaime Terrats.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { Hono } from 'hono';
import { readWorkspaceConfig } from '../../services/workspace-config.js';
import { listTasks } from '../../services/task-state.js';
import { listWorkflowRuns } from '../../services/workflow-state.js';
import { findStaleFiles } from '../../util/command-helpers.js';
import { getWorkspaceCwd } from '../middleware/workspace.js';
import { PLUGIN_VERSION } from '../../version.js';
// Cache npm registry check for 24h
let cachedLatest = null;
let cachedAt = 0;
const CACHE_TTL_MS = 24 * 60 * 60 * 1000;
export function statusRoute(cwd) {
    const app = new Hono();
    app.get('/', (c) => {
        const dir = getWorkspaceCwd(c, cwd);
        const config = readWorkspaceConfig(dir);
        const staleFiles = findStaleFiles(dir);
        const tasks = listTasks(dir, {});
        const workflows = listWorkflowRuns(dir);
        const taskCounts = {
            open: tasks.filter((t) => t.status === 'open').length,
            claimed: tasks.filter((t) => t.status === 'claimed').length,
            done: tasks.filter((t) => t.status === 'done').length,
        };
        return c.json({
            profiles: config?.profiles ?? [],
            tools: config?.tools ?? [],
            mode: config?.mode ?? null,
            orchestration: config?.orchestration?.enabled ?? false,
            staleness: { count: staleFiles.length, files: staleFiles },
            taskCounts,
            workflowCount: workflows.length,
        });
    });
    app.get('/version-check', async (c) => {
        const current = PLUGIN_VERSION;
        let latest = current;
        const now = Date.now();
        if (cachedLatest && now - cachedAt < CACHE_TTL_MS) {
            latest = cachedLatest;
        }
        else {
            try {
                const res = await fetch('https://registry.npmjs.org/@jterrats/setup-agents/latest', {
                    signal: AbortSignal.timeout(5000),
                });
                if (res.ok) {
                    const data = (await res.json());
                    latest = data.version;
                    cachedLatest = latest;
                    cachedAt = now;
                }
            }
            catch {
                /* offline or timeout — keep current as latest */
            }
        }
        const updateAvailable = latest !== current && latest > current;
        return c.json({
            current,
            latest,
            updateAvailable,
            command: 'sf plugins install @jterrats/setup-agents@latest',
        });
    });
    return app;
}
//# sourceMappingURL=status.js.map