#!/usr/bin/env node
import {
  CostTracker
} from "./chunk-7D67AR56.js";
import {
  formatCost
} from "./chunk-BLRPRWZX.js";
import {
  ConfigManager
} from "./chunk-WLMBMRIA.js";
import "./chunk-TZQHYZKT.js";
import "./chunk-2DVGTYZG.js";
import "./chunk-PDX44BCA.js";

// src/cli/usage.ts
function num(n) {
  return n.toLocaleString("en-US");
}
async function runUsageCli(options = {}) {
  const chalk = (await import("chalk")).default;
  const config = new ConfigManager();
  const tracker = new CostTracker(config.getConfigDir());
  const sinceDays = options.days ? Math.max(1, parseInt(options.days, 10) || 0) : void 0;
  const month = options.month?.trim();
  if (month && !/^\d{4}-\d{2}$/.test(month)) {
    console.error(`Invalid --month "${month}". Expected YYYY-MM (e.g. 2026-06).`);
    process.exit(1);
  }
  const breakdown = tracker.getUsageBreakdown({ sinceDays, month });
  if (options.json) {
    console.log(JSON.stringify(breakdown, null, 2));
    return;
  }
  const { rows, totals, fromDate, toDate, dayCount } = breakdown;
  let window;
  if (month) window = month;
  else if (sinceDays) window = `last ${sinceDays} day${sinceDays === 1 ? "" : "s"}`;
  else window = "all tracked history";
  console.log();
  console.log(chalk.bold("  \u{1F4B0} Usage by model") + chalk.dim(`  \u2014  ${window}`));
  if (fromDate && toDate) {
    const span = fromDate === toDate ? fromDate : `${fromDate} \u2192 ${toDate}`;
    console.log(chalk.dim(`  ${span}  (${dayCount} day${dayCount === 1 ? "" : "s"} with activity)`));
  }
  console.log();
  if (rows.length === 0) {
    console.log(chalk.dim("  No usage recorded yet."));
    if (sinceDays || month) console.log(chalk.dim("  Try a wider window, or `aicli usage` for all history."));
    console.log(chalk.dim("  (cost history accrues as you chat in the REPL / Web UI; up to 90 days.)"));
    console.log();
    return;
  }
  const nameW = Math.min(42, Math.max(20, ...rows.map((r) => label(r).length)));
  const rule = "  " + chalk.dim("\u2500".repeat(nameW + 7 + 14 + 14 + 13));
  console.log(
    "  " + chalk.bold("Provider / Model".padEnd(nameW)) + chalk.bold("Calls".padStart(7)) + chalk.bold("Input".padStart(14)) + chalk.bold("Output".padStart(14)) + chalk.bold("Cost".padStart(13))
  );
  console.log(rule);
  for (const r of rows) {
    const isFree = r.cost === 0;
    const namePad = r.provider === "(unattributed)" ? chalk.dim("(unattributed, pre-v0.4.184)".padEnd(nameW)) : chalk.cyan(label(r).padEnd(nameW));
    console.log(
      "  " + namePad + chalk.white(num(r.calls).padStart(7)) + chalk.white(num(r.inputTokens).padStart(14)) + chalk.white(num(r.outputTokens).padStart(14)) + (isFree ? chalk.dim("$0.00".padStart(13)) : chalk.cyan(formatCost(r.cost).padStart(13)))
    );
  }
  console.log(rule);
  console.log(
    "  " + chalk.bold("Total".padEnd(nameW)) + chalk.bold(num(totals.calls).padStart(7)) + chalk.bold(num(totals.inputTokens).padStart(14)) + chalk.bold(num(totals.outputTokens).padStart(14)) + chalk.bold.cyan(formatCost(totals.cost).padStart(13))
  );
  if (totals.cacheReadTokens > 0) {
    console.log(chalk.dim(`  (incl. ${num(totals.cacheReadTokens)} cached-read tokens billed at the cache rate)`));
  }
  console.log();
}
function label(r) {
  return r.model ? `${r.provider}/${r.model}` : r.provider;
}
export {
  runUsageCli
};
