package web

import (
	"testing"
)

func TestGetRegionalLanguageName(t *testing.T) {
	// Initialize locale cache
	LoadLocales()

	tests := []struct {
		name     string
		locale   string
		expected string
	}{
		{
			name:     "Japanese - no region",
			locale:   "ja",
			expected: "Japanese",
		},
		{
			name:     "Spanish - Latin America",
			locale:   "es-419",
			expected: "Spanish (Latin America)", // Language name already contains region in data
		},
		{
			name:     "English - US",
			locale:   "en",
			expected: "English",
		},
		{
			name:     "Russian - Belarus",
			locale:   "ru-BY",
			expected: "Russian - Belarus",
		},
		{
			name:     "Arabic - no region",
			locale:   "ar",
			expected: "Arabic",
		},
		{
			name:     "Chinese - Simplified",
			locale:   "zh-CN",
			expected: "Chinese - China",
		},
		{
			name:     "Unknown locale",
			locale:   "xx-YY",
			expected: "xx-YY",
		},
		{
			name:     "Empty locale",
			locale:   "",
			expected: "",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := GetRegionalLanguageName(tt.locale)
			if result != tt.expected {
				t.Errorf("GetRegionalLanguageName(%q) = %q, want %q", tt.locale, result, tt.expected)
			}
		})
	}
}

func TestGetRegionalNativeName(t *testing.T) {
	// Initialize locale cache
	LoadLocales()

	tests := []struct {
		name     string
		locale   string
		expected string
	}{
		{
			name:     "Japanese - no region",
			locale:   "ja",
			expected: "日本語 (にほんご)",
		},
		{
			name:     "Spanish - Latin America",
			locale:   "es-419",
			expected: "Español (América Latina)", // Language name already contains region in data
		},
		{
			name:     "English - US",
			locale:   "en",
			expected: "English",
		},
		{
			name:     "Russian - Belarus",
			locale:   "ru-BY",
			expected: "Русский - Беларусь",
		},
		{
			name:     "Arabic - no region",
			locale:   "ar",
			expected: "العربية",
		},
		{
			name:     "Chinese - Simplified",
			locale:   "zh-CN",
			expected: "中文 (Zhōngwén) - 中國 / 中国 / Zhōngguó", // Complex multi-script native name
		},
		{
			name:     "Unknown locale",
			locale:   "xx-YY",
			expected: "xx-YY",
		},
		{
			name:     "Empty locale",
			locale:   "",
			expected: "",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := GetRegionalNativeName(tt.locale)
			if result != tt.expected {
				t.Errorf("GetRegionalNativeName(%q) = %q, want %q", tt.locale, result, tt.expected)
			}
		})
	}
}

func TestGetLanguageCountryCode(t *testing.T) {
	// Initialize locale cache
	LoadLocales()

	tests := []struct {
		name     string
		locale   string
		expected string
	}{
		{
			name:     "English - default to US",
			locale:   "en",
			expected: "US",
		},
		{
			name:     "Spanish - default to ES",
			locale:   "es",
			expected: "ES",
		},
		{
			name:     "Arabic - default to SA",
			locale:   "ar",
			expected: "SA",
		},
		{
			name:     "Japanese - default to JP",
			locale:   "ja",
			expected: "JP",
		},
		{
			name:     "Spanish - Latin America",
			locale:   "es-419",
			expected: "419", // This is correct for es-419 (not a country)
		},
		{
			name:     "Russian - Belarus",
			locale:   "ru-BY",
			expected: "BY",
		},
		{
			name:     "Chinese - Simplified",
			locale:   "zh-CN",
			expected: "CN",
		},
		{
			name:     "Unknown locale",
			locale:   "xx-YY",
			expected: "",
		},
		{
			name:     "Empty locale",
			locale:   "",
			expected: "",
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			result := GetLanguageCountryCode(tt.locale)
			if result != tt.expected {
				t.Errorf("GetLanguageCountryCode(%q) = %q, want %q", tt.locale, result, tt.expected)
			}
		})
	}
}

func TestDefaultCountryCodes(t *testing.T) {
	// Test that default country codes are set for common languages
	expectedDefaults := map[string]string{
		"en": "US",
		"es": "ES",
		"ar": "SA",
		"ja": "JP",
		"zh": "CN",
		"fr": "FR",
		"de": "DE",
		"ru": "RU",
		"pt": "BR",
		"it": "IT",
		"ko": "KR",
	}

	for langCode, expectedCountry := range expectedDefaults {
		t.Run(langCode, func(t *testing.T) {
			result := GetLanguageCountryCode(langCode)
			if result != expectedCountry {
				t.Errorf("GetLanguageCountryCode(%q) = %q, want %q", langCode, result, expectedCountry)
			}
		})
	}
}
