package batch

import "sub2api-cn-relay-manager/internal/probe"

type ChannelPatchContract struct {
	ModelMapping       map[string]string
	ModelPricing       map[string]any
	RestrictModels     bool
	BillingModelSource string
}

func ModelMappingDelta(existing map[string]string, discoveredAliases map[string]probe.AliasResult) ChannelPatchContract {
	modelMapping := make(map[string]string, len(existing)+len(discoveredAliases)*2)
	modelPricing := make(map[string]any, len(existing)+len(discoveredAliases))

	for raw, canonical := range existing {
		modelMapping[raw] = canonical
		modelPricing[canonical] = map[string]any{"billing_mode": "token"}
	}

	for _, alias := range discoveredAliases {
		if alias.Canonical == "" {
			continue
		}
		if alias.Raw != "" {
			modelMapping[alias.Raw] = alias.Canonical
		}
		if alias.Normalized != "" {
			modelMapping[alias.Normalized] = alias.Canonical
		}
		modelMapping[alias.Canonical] = alias.Canonical
		modelPricing[alias.Canonical] = map[string]any{"billing_mode": "token"}
	}

	return ChannelPatchContract{
		ModelMapping:       modelMapping,
		ModelPricing:       modelPricing,
		RestrictModels:     true,
		BillingModelSource: "channel_mapped",
	}
}
