package batch

import (
	"testing"

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

func TestModelMappingDelta(t *testing.T) {
	t.Parallel()

	t.Run("preserves existing entries", func(t *testing.T) {
		t.Parallel()

		got := ModelMappingDelta(
			map[string]string{"deepseek-v4-pro": "deepseek-v4-pro"},
			map[string]probe.AliasResult{
				"deepseek-v4-pro": {Raw: "deepseek-ai/DeepSeek-V4-Pro", Canonical: "deepseek-v4-pro"},
			},
		)
		if got.ModelMapping["deepseek-v4-pro"] != "deepseek-v4-pro" {
			t.Fatalf("existing model mapping lost: %#v", got.ModelMapping)
		}
	})

	t.Run("adds raw aliases mapped to canonical ids", func(t *testing.T) {
		t.Parallel()

		got := ModelMappingDelta(
			map[string]string{"kimi-k2.6": "kimi-2.6"},
			map[string]probe.AliasResult{
				"kimi-k2.6": {Raw: "Kimi-K2.6", Canonical: "kimi-2.6"},
			},
		)
		if got.ModelMapping["Kimi-K2.6"] != "kimi-2.6" {
			t.Fatalf("ModelMapping[Kimi-K2.6] = %q, want kimi-2.6", got.ModelMapping["Kimi-K2.6"])
		}
	})

	t.Run("always sets canonical patch flags", func(t *testing.T) {
		t.Parallel()

		got := ModelMappingDelta(nil, map[string]probe.AliasResult{
			"deepseek-v4-pro": {Raw: "deepseek-ai/DeepSeek-V4-Pro", Canonical: "deepseek-v4-pro"},
		})
		if !got.RestrictModels {
			t.Fatal("RestrictModels = false, want true")
		}
		if got.BillingModelSource != "channel_mapped" {
			t.Fatalf("BillingModelSource = %q, want channel_mapped", got.BillingModelSource)
		}
		if got.ModelPricing["deepseek-v4-pro"] == nil {
			t.Fatalf("ModelPricing = %#v, want canonical entry", got.ModelPricing)
		}
	})
}
