package service

import (
	"strings"

	"github.com/seifghazi/claude-code-monitor/internal/model"
)

func modelFilterPattern(modelFilter string, escaper func(string) string) (string, bool) {
	normalized := strings.TrimSpace(strings.ToLower(modelFilter))
	if normalized == "" || normalized == "all" {
		return "", false
	}

	return "%" + escaper(normalized) + "%", true
}

func addUsageStats(stats *model.UsageStats, modelName string, usage *model.AnthropicUsage) {
	if stats == nil || usage == nil {
		return
	}

	inputTokens := int64(usage.InputTokens)
	outputTokens := int64(usage.OutputTokens)
	cacheTokens := int64(usage.CacheCreationInputTokens + usage.CacheReadInputTokens)

	stats.TotalRequests++
	stats.TotalInputTokens += inputTokens
	stats.TotalOutputTokens += outputTokens
	stats.TotalCacheTokens += cacheTokens

	modelStats := stats.RequestsByModel[modelName]
	modelStats.RequestCount++
	modelStats.InputTokens += inputTokens
	modelStats.OutputTokens += outputTokens
	modelStats.CacheTokens += cacheTokens
	stats.RequestsByModel[modelName] = modelStats
}
