using System.Globalization;

namespace ClaudeDo.Worker.Usage.TokenTracker;

/// <summary>
/// The exact CLI invocations we rely on. <c>--no-git</c> matters: without it TokenTracker runs
/// <c>git log</c> inside every session's working directory, which is both slower and touches
/// repositories we have no business touching. We never invoke <c>init</c> — that would write
/// hooks into the user's global <c>~/.claude/settings.json</c> and switch on cloud sync.
/// The date range is passed for correctness, not economy: v0.88.4 accepts it but returns the
/// full history regardless, so <see cref="TokenTrackerAggregator"/> does the real filtering.
/// </summary>
public static class TokenTrackerArgs
{
    public const string Command = "tokentracker";
    public const string NpmCommand = "npm";
    public const string PackageName = "tokentracker-cli";

    /// <summary>TokenTracker's own default dashboard port. It is <b>not</b> reliably free —
    /// Windows Delivery Optimization listens on 7680 — and <c>serve</c> does not fall back on its
    /// own, it just exits with "port is still in use", so we scan
    /// <see cref="DashboardPortSpan"/> ports upward from here.</summary>
    public const int DefaultDashboardPort = 7680;

    public const int DashboardPortSpan = 10;

    public static string[] Export(DateOnly from, DateOnly to) =>
    [
        "sessions",
        "--from", from.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
        "--to", to.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture),
        "--no-git",
        "--format", "json",
    ];

    public static string[] Version() => ["-v"];

    /// <summary>The local dashboard server. <c>--no-open</c> because we open the browser
    /// ourselves once the port answers (the CLI would open it before the server is up), and
    /// <c>--no-sync</c> for the same reason we never call <c>init</c>: we do not push the user's
    /// session data to TokenTracker's cloud on their behalf.</summary>
    public static string[] Serve(int port) =>
    [
        "serve",
        "--port", port.ToString(CultureInfo.InvariantCulture),
        "--no-open",
        "--no-sync",
    ];

    public static string[] Install() => ["i", "-g", PackageName];
}
