package utils

import "testing"

func TestIsValidPrefix(t *testing.T) {
	tests := []struct {
		name     string
		prefix   string
		expected bool
	}{
		{name: "valid 2026-03", prefix: "2026-03", expected: true},
		{name: "valid 2026-01", prefix: "2026-01", expected: true},
		{name: "valid 1999-12", prefix: "1999-12", expected: true},
		{name: "invalid too short", prefix: "2026-3", expected: false},
		{name: "invalid too long", prefix: "2026-003", expected: false},
		{name: "invalid no dash", prefix: "202603", expected: false},
		{name: "invalid letters in year", prefix: "ab-03", expected: false},
		{name: "invalid letters in month", prefix: "2026-ab", expected: false},
		{name: "invalid month > 12", prefix: "2026-13", expected: true},  // 只校验格式，不校验月份合理性
		{name: "empty string", prefix: "", expected: false},
	}

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

func TestIsChineseName(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected bool
	}{
		{name: "pure Chinese name", input: "王旗", expected: true},
		{name: "Chinese name with title", input: "王旗老师", expected: true},
		{name: "two char Chinese", input: "王明", expected: true},
		{name: "three char Chinese", input: "王小明", expected: true},
		{name: "contains digit", input: "王旗1", expected: false},
		{name: "contains English lower", input: "wang旗", expected: false},
		{name: "contains English upper", input: "WANG旗", expected: false},
		{name: "mixed Chinese and digit", input: "王1明", expected: false},
		{name: "empty string", input: "", expected: true},
	}

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

func TestConvertTitleByLocale(t *testing.T) {
	tests := []struct {
		name     string
		title    string
		locale   string
		expected string
	}{
		{
			name:     "EN Data Report",
			title:    "2026-03王旗老师数据报告",
			locale:   "EN",
			expected: "2026-03 Wang Qi Data Report",
		},
		{
			name:     "EN Competitor Report",
			title:    "2026-03王旗老师竞品报告",
			locale:   "EN",
			expected: "2026-03 Wang Qi Competitor Report",
		},
		{
			name:     "zh-TW Data Report",
			title:    "2026-03王旗老师数据报告",
			locale:   "zh-TW",
			expected: "2026-03王旗數據報告",
		},
		{
			name:     "zh-TW Competitor Report",
			title:    "2026-03王旗老师竞品报告",
			locale:   "zh-TW",
			expected: "2026-03王旗競合報告",
		},
		{
			name:     "de-DE Data Report",
			title:    "2026-03王旗老师数据报告",
			locale:   "de-DE",
			expected: "2026-03 Wang Qi Datenbericht",
		},
		{
			name:     "de-DE Competitor Report",
			title:    "2026-03王旗老师竞品报告",
			locale:   "de-DE",
			expected: "2026-03 Wang Qi Wettbewerbsanalyse",
		},
		{
			name:     "ja-JP Data Report",
			title:    "2026-03王旗老师数据报告",
			locale:   "ja-JP",
			expected: "2026-03 Wang Qi データレポート",
		},
		{
			name:     "ja-JP Competitor Report",
			title:    "2026-03王旗老师竞品报告",
			locale:   "ja-JP",
			expected: "2026-03 Wang Qi 競合分析レポート",
		},
		{
			name:     "zh-CN returns original",
			title:    "2026-03王旗老师数据报告",
			locale:   "zh-CN",
			expected: "2026-03王旗老师数据报告",
		},
		{
			name:     "invalid prefix returns original",
			title:    "202603王旗老师数据报告",
			locale:   "EN",
			expected: "202603王旗老师数据报告",
		},
		{
			name:     "invalid personName with digit returns original",
			title:    "2026-03王旗1老师数据报告",
			locale:   "EN",
			expected: "2026-03王旗1老师数据报告",
		},
		{
			name:     "invalid personName with English returns original",
			title:    "2026-03wang老师数据报告",
			locale:   "EN",
			expected: "2026-03wang老师数据报告",
		},
		{
			name:     "title too short returns original",
			title:    "2026-03",
			locale:   "EN",
			expected: "2026-03",
		},
		{
			name:     "unknown reportType returns original",
			title:    "2026-03王旗老师其他报告",
			locale:   "EN",
			expected: "2026-03王旗老师其他报告",
		},
		{
			name:     "empty title returns original",
			title:    "",
			locale:   "EN",
			expected: "",
		},
	}

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

func TestConvertReportName(t *testing.T) {
	tests := []struct {
		name       string
		prefix     string // YYYY-MM
		personName string // 中文姓名
		reportType string // 数据报告 或 竞品报告
		locale     string
		expected   string
	}{
		{
			name:       "EN Data Report",
			prefix:     "2026-03",
			personName: "王旗老师",
			reportType: "数据报告",
			locale:     "EN",
			expected:   "2026-03 Wang Qi Data Report",
		},
		{
			name:       "EN Competitor Report",
			prefix:     "2026-03",
			personName: "王旗老师",
			reportType: "竞品报告",
			locale:     "EN",
			expected:   "2026-03 Wang Qi Competitor Report",
		},
		{
			name:       "zh-TW Data Report",
			prefix:     "2026-03",
			personName: "王旗老师",
			reportType: "数据报告",
			locale:     "zh-TW",
			expected:   "2026-03王旗數據報告",
		},
		{
			name:       "zh-TW Competitor Report",
			prefix:     "2026-03",
			personName: "王旗老师",
			reportType: "竞品报告",
			locale:     "zh-TW",
			expected:   "2026-03王旗競合報告",
		},
		{
			name:       "de-DE Data Report",
			prefix:     "2026-03",
			personName: "王旗老师",
			reportType: "数据报告",
			locale:     "de-DE",
			expected:   "2026-03 Wang Qi Datenbericht",
		},
		{
			name:       "de-DE Competitor Report",
			prefix:     "2026-03",
			personName: "王旗老师",
			reportType: "竞品报告",
			locale:     "de-DE",
			expected:   "2026-03 Wang Qi Wettbewerbsanalyse",
		},
		{
			name:       "ja-JP Data Report",
			prefix:     "2026-03",
			personName: "王旗老师",
			reportType: "数据报告",
			locale:     "ja-JP",
			expected:   "2026-03 Wang Qi データレポート",
		},
		{
			name:       "ja-JP Competitor Report",
			prefix:     "2026-03",
			personName: "王旗老师",
			reportType: "竞品报告",
			locale:     "ja-JP",
			expected:   "2026-03 Wang Qi 競合分析レポート",
		},
		{
			name:       "zh-CN Data Report (no conversion)",
			prefix:     "2026-03",
			personName: "王旗老师",
			reportType: "数据报告",
			locale:     "zh-CN",
			expected:   "2026-03王旗老师数据报告",
		},
		{
			name:       "zh-CN Competitor Report (no conversion)",
			prefix:     "2026-03",
			personName: "王旗老师",
			reportType: "竞品报告",
			locale:     "zh-CN",
			expected:   "2026-03王旗老师竞品报告",
		},
	}

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

func TestConvertNameToPinyin(t *testing.T) {
	tests := []struct {
		name     string
		input    string
		expected string
	}{
		{
			name:     "simple name",
			input:    "王旗",
			expected: "Wang Qi",
		},
		{
			name:     "name with title",
			input:    "王旗老师",
			expected: "Wang Qi",
		},
		{
			name:     "two word name with title",
			input:    "王小明老师",
			expected: "Wang Xiao Ming",
		},
	}

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