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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package cmd

import (
	"fmt"
	"os"
	"plandex-cli/api"
	"plandex-cli/auth"
	"plandex-cli/lib"
	"plandex-cli/term"
	shared "plandex-shared"
	"sort"
	"strconv"
	"strings"
	"time"
	"unicode"

	"github.com/eiannone/keyboard"
	"github.com/fatih/color"
	"github.com/olekukonko/tablewriter"
	"github.com/shopspring/decimal"
	"github.com/spf13/cobra"
)

const MaxCreditsLogPageSize = 500

var logCreditsPageSize int
var logCreditsPage int

var logCreditsDebitsOnly bool
var logCreditsCreditsOnly bool

var showUsageLog bool

var creditsSession bool
var creditsToday bool
var creditsMonth bool
var creditsCurrentPlan bool

var usageCmd = &cobra.Command{
	Use:   "usage",
	Short: "Display credits balance and usage report",
	Run:   usage,
}

func init() {
	RootCmd.AddCommand(usageCmd)

	usageCmd.Flags().BoolVar(&showUsageLog, "log", false, "Show usage log")
	usageCmd.Flags().IntVarP(&logCreditsPageSize, "page-size", "s", 100, "Number of transactions to display per page")
	usageCmd.Flags().IntVarP(&logCreditsPage, "page", "p", 1, "Page number to display")
	usageCmd.Flags().BoolVar(&logCreditsDebitsOnly, "debits", false, "Show only debits in the log")
	usageCmd.Flags().BoolVar(&logCreditsCreditsOnly, "purchases", false, "Show only purchases in the log")

	usageCmd.Flags().BoolVar(&creditsToday, "today", false, "Show usage for today")
	usageCmd.Flags().BoolVar(&creditsMonth, "month", false, "Show usage for current billing month")
	usageCmd.Flags().BoolVar(&creditsCurrentPlan, "plan", false, "Show usage for the current plan")
}

func usage(cmd *cobra.Command, args []string) {
	if showUsageLog {
		showLog(cmd, args)
	} else {
		showUsage()
	}
}

func showUsage() {
	auth.MustResolveAuthWithOrg()

	term.StartSpinner("")

	if !(creditsSession || creditsToday || creditsMonth || creditsCurrentPlan) {
		if os.Getenv("PLANDEX_REPL_SESSION_ID") != "" {
			creditsSession = true
		} else {
			creditsToday = true
		}
	}

	var sessionId string
	if creditsSession {
		sessionId = os.Getenv("PLANDEX_REPL_SESSION_ID")
		if sessionId == "" {
			term.OutputErrorAndExit("Session ID is not set. The --session flag should be used in the Plandex REPL.")
		}
	}

	var dayStart *time.Time
	if creditsToday {
		now := time.Now()
		midnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
		dayStart = &midnight
	}

	var planId string
	var currentPlanName string
	if creditsCurrentPlan {
		lib.MustResolveProject()
		planId = lib.CurrentPlanId
		plan, apiErr := api.Client.GetPlan(planId)
		if apiErr != nil {
			term.OutputErrorAndExit("Error getting plan: %v", apiErr)
		}
		currentPlanName = plan.Name
	}

	req := shared.CreditsLogRequest{
		SessionId: sessionId,
		DayStart:  dayStart,
		Month:     creditsMonth,
		PlanId:    planId,
	}

	res, apiErr := api.Client.GetCreditsSummary(req)
	term.StopSpinner()

	if apiErr != nil {
		term.OutputErrorAndExit("Error getting credits summary: %v", apiErr)
	}

	builder := strings.Builder{}

	balance := res.Balance
	balanceStr := formatSpend(balance)

	spendLbl := "πŸ’Έ Spent"
	if creditsSession {
		spendLbl += " This Session"
	} else if creditsToday {
		spendLbl += " Today"
	} else if creditsMonth {
		spendLbl += " This Billing Month"
		spendLbl += fmt.Sprintf(" (since %s)", res.MonthStart.Format("Jan 2"))
	} else if creditsCurrentPlan {
		spendLbl += fmt.Sprintf(" On Plan πŸ“‹ %s", currentPlanName)
	}

	var spendStr string
	if res.TotalSpend.IsZero() {
		spendStr = "$0.00"
	} else {
		spendStr = formatSpend(res.TotalSpend)
	}

	table := tablewriter.NewWriter(&builder)
	table.SetAutoWrapText(false)
	table.SetHeader([]string{"πŸ’° Current Balance", spendLbl})
	table.Append([]string{balanceStr, spendStr})
	table.Render()
	fmt.Fprintln(&builder)

	if !res.CacheSavings.IsZero() {
		table := tablewriter.NewWriter(&builder)
		table.SetAutoWrapText(false)
		table.SetHeader([]string{"🎯 Cache Savings"})
		table.Append([]string{formatSpend(res.CacheSavings)})
		table.Render()
		fmt.Fprintln(&builder)
	}

	amountByStr := map[string]float64{}
	if len(res.ByPlanId) > 0 {
		if !creditsCurrentPlan {
			table := tablewriter.NewWriter(&builder)
			table.SetAutoWrapText(false)
			table.SetHeader([]string{"πŸ“‹ Plan", "πŸ’Έ Spent"})

			rows := [][]string{}

			for id, spend := range res.ByPlanId {
				name := res.PlanNamesById[id]
				spendStr := formatSpend(spend)
				rows = append(rows, []string{name, spendStr})
				amountByStr[spendStr] = spend.InexactFloat64()
			}
			sort.Slice(rows, func(i, j int) bool {
				return amountByStr[rows[i][1]] > amountByStr[rows[j][1]]
			})
			for _, row := range rows {
				table.Append(row)
			}

			table.Render()
			fmt.Fprintln(&builder)
		}
	}

	if len(res.ByPurpose) > 0 {
		table = tablewriter.NewWriter(&builder)
		table.SetAutoWrapText(false)
		table.SetHeader([]string{"⚑️ Purpose", "πŸ’Έ Spent"})

		rows := [][]string{}
		for name, spend := range res.ByPurpose {
			spendStr := formatSpend(spend)
			rows = append(rows, []string{name, spendStr})
			amountByStr[spendStr] = spend.InexactFloat64()
		}
		sort.Slice(rows, func(i, j int) bool {
			return amountByStr[rows[i][1]] > amountByStr[rows[j][1]]
		})
		for _, row := range rows {
			table.Append(row)
		}

		table.Render()
		fmt.Fprintln(&builder)
	}

	if len(res.ByModelName) > 0 {
		table = tablewriter.NewWriter(&builder)
		table.SetAutoWrapText(false)
		table.SetHeader([]string{"πŸ€– Model", "πŸ’Έ Spent"})

		rows := [][]string{}
		for name, spend := range res.ByModelName {
			spendStr := formatSpend(spend)
			rows = append(rows, []string{name, spendStr})
			amountByStr[spendStr] = spend.InexactFloat64()
		}
		sort.Slice(rows, func(i, j int) bool {
			return amountByStr[rows[i][1]] > amountByStr[rows[j][1]]
		})
		for _, row := range rows {
			table.Append(row)
		}
		table.Render()
		fmt.Fprintln(&builder)
	}

	term.PageOutput(builder.String())

	term.PrintCmds("", "usage", "billing")
}

func showLog(cmd *cobra.Command, args []string) {
	auth.MustResolveAuthWithOrg()

	if !(creditsSession || creditsToday || creditsMonth || creditsCurrentPlan) {
		if os.Getenv("PLANDEX_REPL_SESSION_ID") != "" {
			creditsSession = true
		} else {
			creditsToday = true
		}
	}

	term.StartSpinner("")

	var transactionType shared.CreditsTransactionType

	if logCreditsDebitsOnly {
		transactionType = shared.CreditsTransactionTypeDebit
	} else if logCreditsCreditsOnly {
		transactionType = shared.CreditsTransactionTypeCredit
	}

	var sessionId string
	if creditsSession {
		sessionId = os.Getenv("PLANDEX_REPL_SESSION_ID")
		if sessionId == "" {
			term.OutputErrorAndExit("Session ID is not set. The --session flag should be used in the Plandex REPL.")
		}
	}

	var dayStart *time.Time
	if creditsToday {
		now := time.Now()
		midnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
		dayStart = &midnight
	}

	var planId string
	var planName string
	if creditsCurrentPlan {
		lib.MustResolveProject()
		planId = lib.CurrentPlanId

		plan, err := api.Client.GetPlan(planId)
		if err != nil {
			term.OutputErrorAndExit("Error getting plan: %v", err)
			return
		}
		planName = plan.Name
	}

	req := shared.CreditsLogRequest{
		TransactionType: transactionType,
		SessionId:       sessionId,
		DayStart:        dayStart,
		Month:           creditsMonth,
		PlanId:          planId,
	}

	res, apiErr := api.Client.GetCreditsTransactions(logCreditsPageSize, logCreditsPage, req)
	term.StopSpinner()

	if apiErr != nil {
		term.OutputErrorAndExit("Error getting credits transactions: %v", apiErr)
		return
	}

	transactions := res.Transactions

	if len(transactions) == 0 {
		lbl := "πŸ€·β€β™‚οΈ No usage"
		if sessionId != "" {
			lbl = "πŸ€·β€β™‚οΈ No usage so far this session"
		} else if creditsToday {
			tz, _ := time.Now().Zone()
			lbl = fmt.Sprintf("πŸ€·β€β™‚οΈ No usage so far today (since midnight %s)", tz)
		} else if creditsMonth {
			lbl = fmt.Sprintf("πŸ€·β€β™‚οΈ No usage so far this billing month (since %s)", res.MonthStart.Format("Jan 2"))
		} else if creditsCurrentPlan {
			lbl = "πŸ€·β€β™‚οΈ No usage so far for current plan πŸ‘‰ " + planName
		}
		fmt.Println(lbl)
		return
	}

	tableString := &strings.Builder{}
	table := tablewriter.NewWriter(tableString)
	table.SetAutoWrapText(false)
	table.SetHeader([]string{"Amount", "Balance", "Transaction"})

	for _, transaction := range transactions {
		var sign string
		var c color.Attribute

		desc := transaction.CreatedAt.Local().Format("2006-01-02 15:04:05.000 EST") + "\n"

		if transaction.TransactionType == "debit" {
			sign = "-"
			c = term.ColorHiRed

			if transaction.DebitPlanId != nil {
				planName := res.PlanNamesById[*transaction.DebitPlanId]
				desc += fmt.Sprintf("Plan β†’ %s\n", planName)
			}

			surchargePct := transaction.DebitSurcharge.Div(*transaction.DebitBaseAmount)

			inputPrice := transaction.DebitModelInputPricePerToken.Mul(decimal.NewFromInt(1000000)).Mul(surchargePct.Add(decimal.NewFromInt(1))).StringFixed(4)
			outputPrice := transaction.DebitModelOutputPricePerToken.Mul(decimal.NewFromInt(1000000)).Mul(surchargePct.Add(decimal.NewFromInt(1))).StringFixed(4)

			var cacheDiscountStr string
			var cacheDiscountPct float64
			if transaction.DebitCacheDiscount != nil {
				cacheDiscountStr = transaction.DebitCacheDiscount.StringFixed(4)
				totalAmount := transaction.DebitBaseAmount.Add(*transaction.DebitCacheDiscount)
				cacheDiscountPct = transaction.DebitCacheDiscount.Div(totalAmount).Mul(decimal.NewFromInt(100)).InexactFloat64()
			}

			for i := 0; i < 2; i++ {
				inputPrice = strings.TrimSuffix(inputPrice, "0")
				outputPrice = strings.TrimSuffix(outputPrice, "0")
				cacheDiscountStr = strings.TrimSuffix(cacheDiscountStr, "0")
			}

			desc += fmt.Sprintf("⚑️ %s\n", *transaction.DebitPurpose)
			desc += fmt.Sprintf("🧠 %s\n", transaction.ModelString())
			desc += fmt.Sprintf("πŸ’³ Price β†’ $%s input / $%s output per 1M\n", inputPrice, outputPrice)
			desc += fmt.Sprintf("πŸͺ™ Used β†’ %d input / %d output\n", *transaction.DebitInputTokens, *transaction.DebitOutputTokens)

			if cacheDiscountStr != "" {
				desc += fmt.Sprintf("🎯 Cache discount β†’ $%s (%d%%)\n", cacheDiscountStr, int(cacheDiscountPct))
			}

		} else {
			sign = "+"
			c = term.ColorHiGreen

			switch *transaction.CreditType {
			case shared.CreditTypeGrant:
				desc += "Monthly subscription payment"
			case shared.CreditTypeTrial:
				desc += "Started trial"
			case shared.CreditTypePurchase:
				desc += "Purchased credits"
			case shared.CreditTypeSwitch:
				desc += "Switched to Integrated Models mode"
			}

			desc += "\n"
		}

		amountStr := transaction.Amount.StringFixed(6)
		for i := 0; i < 4; i++ {
			amountStr = strings.TrimSuffix(amountStr, "0")
		}

		balanceStr := transaction.EndBalance.StringFixed(4)
		for i := 0; i < 2; i++ {
			balanceStr = strings.TrimSuffix(balanceStr, "0")
		}

		table.Append([]string{
			color.New(c).Sprint(sign + "$" + amountStr),
			"$" + balanceStr,

			desc,
		})
	}

	table.Render()

	var output string
	var pageLine string

	if res.NumPages > 1 {
		pageLine = fmt.Sprintf("Page size %d. Showing page %d of %d", logCreditsPageSize, logCreditsPage, res.NumPages)
		if res.NumPagesMax {
			pageLine = "+"
		}
		output = pageLine + "\n\n" + tableString.String()
	} else {
		output = tableString.String()
	}

	term.PageOutput(output)

	var inputFn func()
	inputFn = func() {
		fmt.Println("\n" + pageLine)

		prompts := []string{}

		if res.NumPages > 1 && logCreditsPage < res.NumPages {
			prompts = append(prompts, "Press 'n' for next page")
		}

		if logCreditsPage > 1 {
			prompts = append(prompts, "Press 'p' for previous page")
		}

		prompts = append(prompts, "Type any number and press enter to jump to a page")

		prompts = append(prompts, "Press 'q' to quit")

		color.New(term.ColorHiMagenta, color.Bold).Println(strings.Join(prompts, "\n"))
		color.New(term.ColorHiMagenta, color.Bold).Print("> ")

		char, _, err := term.GetUserKeyInput()

		if err != nil {
			term.OutputErrorAndExit("Failed to get user input: %v", err)
		}

		// Check if the input is a digit
		if unicode.IsDigit(char) {
			var numberInput strings.Builder
			numberInput.WriteRune(char)

			fmt.Print(string(char)) // Show the initial digit

			for {
				char, key, err := term.GetUserKeyInput()
				if err != nil {
					term.OutputErrorAndExit("Failed to get user input: %v", err)
				}

				// If Enter is pressed, commit the input
				if key == keyboard.KeyEnter {
					pageNumber, err := strconv.Atoi(numberInput.String())
					if err != nil {
						fmt.Println("Invalid page number.")
						return
					}

					// Check if the page number is valid
					if pageNumber >= 1 && (pageNumber <= res.NumPages || res.NumPagesMax) {
						logCreditsPage = pageNumber
						showLog(cmd, args) // Re-run the log command with the new page
					} else {
						fmt.Println()
						fmt.Println("Invalid page number.")
						inputFn()
					}
					return
				}

				// If another digit is pressed, add it to the input
				if unicode.IsDigit(char) {
					numberInput.WriteRune(char)
					fmt.Print(string(char)) // Show the digit
				} else if key == keyboard.KeyBackspace || key == keyboard.KeyBackspace2 {
					// Handle backspace
					if numberInput.Len() > 0 {
						// Remove the last rune
						input := numberInput.String()
						numberInput.Reset()
						numberInput.WriteString(input[:len(input)-1])
						fmt.Print("\b \b") // Erase the digit
					}

				} else {
					// Handle invalid input while typing a number
					fmt.Println()
					fmt.Println("\nInvalid input. Please enter a valid page number.")
					inputFn()
					return
				}
			}
		}

		// Handle non-digit hotkeys
		fmt.Print(string(char))
		switch char {
		case 'n':
			if logCreditsPage < res.NumPages || res.NumPagesMax {
				logCreditsPage++
				showLog(cmd, args)
			} else {
				fmt.Println()
				fmt.Println("Already on last page.")
				inputFn()
			}
		case 'p':
			if logCreditsPage > 1 {
				logCreditsPage--
				showLog(cmd, args)
			} else {
				fmt.Println()
				fmt.Println("Already on first page.")
				inputFn()
			}
		case 'q':
			fmt.Println()
			return
		default:
			fmt.Println()
			fmt.Println("Invalid input.")
			inputFn()
		}

	}

	if res.NumPages > 1 {
		inputFn()
	}
}

func formatSpend(spend decimal.Decimal) string {
	if spend.IsZero() {
		return "$0.00"
	}

	spendStr := fmt.Sprintf("$%s", spend.StringFixed(4))
	for i := 0; i < 2; i++ {
		if strings.HasSuffix(spendStr, "0") {
			spendStr = spendStr[:len(spendStr)-1]
		}
	}
	if spendStr == "$0.00" {
		return "<$0.0001"
	}
	return spendStr
}