package probe

import (
	"reflect"
	"testing"
)

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

	t.Run("kimi aliases collapse into one family", func(t *testing.T) {
		t.Parallel()

		variants := []string{"kimi 2.6", "kimi-2.6", "kimi-k2.6", "Kimi-K2.6"}
		for _, variant := range variants {
			if got := CanonicalModelFamily(variant); got != "kimi-2.6" {
				t.Fatalf("CanonicalModelFamily(%q) = %q, want %q", variant, got, "kimi-2.6")
			}
		}
	})

	t.Run("deepseek vendor prefix normalizes away", func(t *testing.T) {
		t.Parallel()

		if got := NormalizeModelID("deepseek-ai/DeepSeek-V4-Pro"); got != "deepseek-v4-pro" {
			t.Fatalf("NormalizeModelID() = %q, want %q", got, "deepseek-v4-pro")
		}
		if got := CanonicalModelID("deepseek-ai/DeepSeek-V4-Pro"); got != "deepseek-v4-pro" {
			t.Fatalf("CanonicalModelID() = %q, want %q", got, "deepseek-v4-pro")
		}
	})

	t.Run("alias table and requested model resolution prefer discovered ids", func(t *testing.T) {
		t.Parallel()

		rawModels := []string{"deepseek-ai/DeepSeek-V4-Pro", "kimi-k2.6"}
		table := BuildAliasTable(rawModels)
		if got := table["deepseek-v4-pro"].Canonical; got != "deepseek-v4-pro" {
			t.Fatalf("alias canonical = %q, want %q", got, "deepseek-v4-pro")
		}

		resolved, ok := ResolveRequestedModel("DeepSeek V4 Pro", rawModels)
		if !ok {
			t.Fatal("ResolveRequestedModel() ok = false, want true")
		}
		if resolved != "deepseek-ai/DeepSeek-V4-Pro" {
			t.Fatalf("ResolveRequestedModel() = %q, want discovered raw id", resolved)
		}
	})

	t.Run("recommend models returns canonical discovered candidates", func(t *testing.T) {
		t.Parallel()

		rawModels := []string{"kimi-k2.6", "deepseek-ai/DeepSeek-V4-Pro"}
		got := RecommendModels([]string{"kimi 2.6", "DeepSeek V4 Pro", "unknown"}, rawModels)
		want := []string{"kimi-k2.6", "deepseek-ai/DeepSeek-V4-Pro"}
		if !reflect.DeepEqual(got, want) {
			t.Fatalf("RecommendModels() = %#v, want %#v", got, want)
		}
	})
}
