// resources/budget-status-resource.ts — MCP resource for planu://budget/status
// Returns budget status aggregated across all registered projects.
// No parameters required.
import { getProjects } from '../storage/global-projects-store.js';
import { listBudgets } from '../storage/budget-store.js';
import { computeBudgetStatus } from '../engine/cost-guardrails.js';
/**
 * MCP resource handler for planu://budget/status
 * Aggregates budget status across all registered projects.
 */
export async function handleBudgetStatusResource() {
    const projects = await getProjects();
    if (projects.length === 0) {
        return JSON.stringify({
            generatedAt: new Date().toISOString(),
            projectCount: 0,
            totalBudgets: 0,
            message: 'No projects registered. Use register_project_path to add projects.',
            projects: [],
        }, null, 2);
    }
    const projectBudgets = await Promise.all(projects.map(async (project) => {
        const budgets = await listBudgets(project.path);
        const statuses = budgets.map((b) => computeBudgetStatus(b));
        return {
            projectPath: project.path,
            budgetCount: budgets.length,
            budgets: statuses.map((s) => ({
                specId: s.specId,
                maxUsdCents: s.maxUsdCents,
                usedUsdCents: s.usedUsdCents,
                remainingUsdCents: s.remainingUsdCents,
                usagePercent: s.usagePercent,
                warningTriggered: s.warningTriggered,
                exhausted: s.exhausted,
                recommendedModel: s.recommendedModel,
            })),
        };
    }));
    const totalBudgets = projectBudgets.reduce((sum, p) => sum + p.budgetCount, 0);
    const overBudgetCount = projectBudgets.reduce((sum, p) => sum + p.budgets.filter((b) => b.exhausted).length, 0);
    return JSON.stringify({
        generatedAt: new Date().toISOString(),
        projectCount: projects.length,
        totalBudgets,
        overBudgetCount,
        projects: projectBudgets,
    }, null, 2);
}
export const budgetStatusResourceDefinition = {
    uri: 'planu://budget/status',
    name: 'Budget Status',
    description: 'Budget status for all registered projects: spec budgets, spend, and over-budget alerts. ' +
        'Read-only. For a specific project or spec use the budget_status tool.',
    mimeType: 'application/json',
};
//# sourceMappingURL=budget-status-resource.js.map