// tools/usage-report.ts — Handler for the usage_report MCP tool.
// Returns a full usage report in JSON or markdown format.
import { usageStore } from '../storage/index.js';
import { filterEventsByPeriod } from '../engine/usage-tracker/core.js';
import { calculateUsageStats, formatStatsAsMarkdown } from '../engine/usage-tracker/stats.js';
import { calculateDaysSinceFirstRun, isTrialActive } from '../engine/usage-tracker/trial.js';
import { getCurrentTier } from './license-gate.js';
import { compactJson, compactResult } from './output-formatter.js';
export async function handleUsageReport(args) {
    const format = args.format ?? 'markdown';
    const tier = await getCurrentTier();
    // Gather data from storage
    const usageLog = await usageStore.getUsageLog();
    const trialState = await usageStore.ensureTrialState();
    const today = new Date().toISOString().slice(0, 10);
    const todayCount = await usageStore.getDailyCallCount(today);
    // Compute context values
    const daysSinceFirstRun = calculateDaysSinceFirstRun(trialState.firstRunAt);
    const trialActive = isTrialActive(trialState.firstRunAt);
    let remainingDailyCalls = -1;
    if (tier === 'free') {
        const dailyLimit = trialActive ? 50 : 30;
        remainingDailyCalls = Math.max(0, dailyLimit - todayCount);
    }
    // Full report uses all events
    const allEvents = filterEventsByPeriod(usageLog.events, 'all');
    const stats = calculateUsageStats(allEvents, 0, 0, tier, daysSinceFirstRun, trialActive, remainingDailyCalls);
    if (format === 'json') {
        return compactResult(compactJson(stats));
    }
    const markdown = formatStatsAsMarkdown(stats);
    return { content: [{ type: 'text', text: markdown }] };
}
//# sourceMappingURL=usage-report.js.map