package grpc

import (
	"strings"
	"testing"

	gamedomain "hyapp/services/game-service/internal/domain/game"
)

func TestPlatformToProtoExposesCallbackSecretForAdmin(t *testing.T) {
	const secret = "leadercc-admin-visible-key"

	got := platformToProto(gamedomain.Platform{
		PlatformCode:             "lingxian",
		CallbackSecretCiphertext: secret,
		CallbackSecretConfigured: true,
	})

	if got.GetCallbackSecret() != secret {
		t.Fatalf("callback_secret = %q, want %q", got.GetCallbackSecret(), secret)
	}
	if !got.GetCallbackSecretSet() {
		t.Fatal("callback_secret_set = false, want true")
	}
}

func TestNormalizePlatformAcceptsProviderAdapters(t *testing.T) {
	// 平台创建入口会先规范化 adapter_type；新增厂商必须在这里放行，否则后台配置页会被 gRPC 直接拒绝。
	for _, adapterType := range []string{gamedomain.AdapterVivaGamesV1, gamedomain.AdapterReyouV1, gamedomain.AdapterZGameV1, gamedomain.AdapterAMGV1} {
		got, err := normalizePlatform(gamedomain.Platform{
			PlatformCode:      strings.TrimSuffix(adapterType, "_v1"),
			PlatformName:      adapterType,
			Status:            gamedomain.StatusActive,
			AdapterType:       adapterType,
			AdapterConfigJSON: "{}",
		})
		if err != nil {
			t.Fatalf("normalizePlatform(adapter=%s) error = %v", adapterType, err)
		}
		if got.AdapterType != adapterType {
			t.Fatalf("adapter_type = %q, want %q", got.AdapterType, adapterType)
		}
	}
}

func TestNormalizePlatformRejectsDemoAdapter(t *testing.T) {
	for _, adapterType := range []string{"", gamedomain.AdapterDemo} {
		_, err := normalizePlatform(gamedomain.Platform{
			PlatformCode:      "demo",
			PlatformName:      "Demo",
			Status:            gamedomain.StatusActive,
			AdapterType:       adapterType,
			AdapterConfigJSON: "{}",
		})
		if err == nil {
			t.Fatalf("normalizePlatform(adapterType=%q) expected error", adapterType)
		}
	}
}
