package types

import "fmt"

// ClaudeCacheCreation1hMultiplier 1小时缓存写入价格相对于5分钟的比例
// https://docs.claude.com/en/docs/build-with-claude/prompt-caching#1-hour-cache-duration
const ClaudeCacheCreation1hMultiplier = 6 / 3.75

type GroupRatioInfo struct {
	GroupRatio        float64
	GroupSpecialRatio float64
	HasSpecialRatio   bool
}

type PriceData struct {
	FreeModel            bool
	ModelPrice           float64
	ModelRatio           float64
	CompletionRatio      float64
	CacheRatio           float64
	CacheCreationRatio   float64
	CacheCreation5mRatio float64
	CacheCreation1hRatio float64
	ImageRatio           float64
	AudioRatio           float64
	AudioCompletionRatio float64
	OtherRatios          map[string]float64
	UsePrice             bool
	Quota                int // 按次计费的最终额度（MJ / Task）
	QuotaToPreConsume    int // 按量计费的预消耗额度
	GroupRatioInfo       GroupRatioInfo
}

// ApplyChannelPricingRatios 将渠道定价的扩展比率应用到 PriceData（非零值覆盖）
// cacheRatio, cacheCreationRatio, imageRatio, audioRatio, audioCompletionRatio
func (p *PriceData) ApplyChannelPricingRatios(cacheRatio, cacheCreationRatio, imageRatio, audioRatio, audioCompletionRatio float64) {
	if cacheRatio != 0 {
		p.CacheRatio = cacheRatio
	}
	if cacheCreationRatio != 0 {
		p.CacheCreationRatio = cacheCreationRatio
		p.CacheCreation5mRatio = cacheCreationRatio
		p.CacheCreation1hRatio = cacheCreationRatio * ClaudeCacheCreation1hMultiplier
	}
	if imageRatio != 0 {
		p.ImageRatio = imageRatio
	}
	if audioRatio != 0 {
		p.AudioRatio = audioRatio
	}
	if audioCompletionRatio != 0 {
		p.AudioCompletionRatio = audioCompletionRatio
	}
}

func (p *PriceData) AddOtherRatio(key string, ratio float64) {
	if p.OtherRatios == nil {
		p.OtherRatios = make(map[string]float64)
	}
	if ratio <= 0 {
		return
	}
	p.OtherRatios[key] = ratio
}

func (p *PriceData) ToSetting() string {
	return fmt.Sprintf("ModelPrice: %f, ModelRatio: %f, CompletionRatio: %f, CacheRatio: %f, GroupRatio: %f, UsePrice: %t, CacheCreationRatio: %f, CacheCreation5mRatio: %f, CacheCreation1hRatio: %f, QuotaToPreConsume: %d, ImageRatio: %f, AudioRatio: %f, AudioCompletionRatio: %f", p.ModelPrice, p.ModelRatio, p.CompletionRatio, p.CacheRatio, p.GroupRatioInfo.GroupRatio, p.UsePrice, p.CacheCreationRatio, p.CacheCreation5mRatio, p.CacheCreation1hRatio, p.QuotaToPreConsume, p.ImageRatio, p.AudioRatio, p.AudioCompletionRatio)
}
