package auth

import "testing"

func TestDefaultHTTPIPGeoProvidersFollowChatapp3Order(t *testing.T) {
	providers := BuildIPGeoProviders([]string{"primary_geo", "fallback_geo_a", "fallback_geo_b", "fallback_geo_c"})
	if len(providers) != 4 {
		t.Fatalf("provider count = %d", len(providers))
	}
	want := []struct {
		name string
		url  string
		path string
	}{
		{name: "primary_geo", url: "https://api.ip.sb/geoip/{ip}", path: "country_code"},
		{name: "fallback_geo_a", url: "https://freeipapi.com/api/json/{ip}", path: "countryCode"},
		{name: "fallback_geo_b", url: "https://ipwhois.app/json/{ip}?format=json", path: "country_code"},
		{name: "fallback_geo_c", url: "https://ip.nc.gy/json?ip={ip}", path: "country.iso_code"},
	}
	for index, expected := range want {
		provider, ok := providers[index].(httpIPGeoProvider)
		if !ok {
			t.Fatalf("provider %s is %T, want httpIPGeoProvider", expected.name, providers[index])
		}
		if provider.name != expected.name || provider.urlTemplate != expected.url || provider.countryJSONPath != expected.path {
			t.Fatalf("provider %d mismatch: %+v", index, provider)
		}
	}
}

func TestCountryFromHTTPGeoPayloadReadsRegisteredCountryFallback(t *testing.T) {
	payload := map[string]any{
		"registered_country": map[string]any{"iso_code": "cn"},
	}
	if got := countryFromHTTPGeoPayload(payload, ""); got != "CN" {
		t.Fatalf("registered_country fallback = %q", got)
	}
}
