import { getEntries } from '../storage/token-ledger-store.js';
import { calculateTokenSavings } from '../engine/token-savings-calculator.js';
import { resolveProjectId, missingProjectIdError } from './resolve-project-id.js';
import { join } from 'node:path';
const DEFAULT_DATA_DIR = join(process.cwd(), 'data');
export async function handleTokenSavingsReport(args) {
    const period = args.period ?? 'month';
    const projectId = resolveProjectId(args);
    if (!projectId) {
        return missingProjectIdError;
    }
    const filter = { period: period };
    const entries = await getEntries(projectId, DEFAULT_DATA_DIR, filter);
    const report = calculateTokenSavings(entries, period);
    const lines = [
        `## Token Savings Report — ${period}`,
        ``,
        `**Entries analyzed:** ${report.totalEntries} (${report.specGuidedEntries} spec-guided, ${report.unstructuredEntries} unstructured)`,
        `**Confidence:** ${report.confidenceLevel}`,
        ``,
    ];
    if (report.savingsPct !== null) {
        lines.push(`**Average tokens (spec-guided):** ${Math.round(report.avgTokensSpecGuided)}`);
        lines.push(`**Average tokens (unstructured):** ${Math.round(report.avgTokensUnstructured)}`);
        lines.push(`**Savings:** ${report.savingsPct.toFixed(1)}% per operation`);
        lines.push(`**Total tokens saved (est.):** ${Math.round(report.estimatedSavingsTotal).toLocaleString()}`);
        if (report.topSavingTools.length > 0) {
            lines.push(``, `**Top tools by savings:**`);
            for (const t of report.topSavingTools) {
                lines.push(`- ${t.tool}: ${t.savingsPct.toFixed(1)}% (${Math.round(t.specGuided)} vs ${Math.round(t.unstructured)} tokens avg)`);
            }
        }
    }
    else {
        lines.push(`Not enough data yet. Use Planu with projectPath on more operations to build a baseline.`);
    }
    return { content: [{ type: 'text', text: lines.join('\n') }] };
}
//# sourceMappingURL=token-savings-handler.js.map