package i18n

import "strings"

const DefaultLang = "TR"

var supported = map[string]struct{}{
	"TR": {},
	"EN": {},
	"DE": {},
	"IT": {},
	"ES": {},
	"RU": {},
	"AR": {},
}

func NormalizeLangCode(raw string) string {
	lang := strings.ToUpper(strings.TrimSpace(raw))
	if _, ok := supported[lang]; ok {
		return lang
	}
	return DefaultLang
}

func ResolveLangCode(queryLangCode, acceptLanguage string) string {
	if lang := NormalizeLangCode(queryLangCode); lang != DefaultLang || strings.EqualFold(strings.TrimSpace(queryLangCode), DefaultLang) {
		return lang
	}

	header := strings.TrimSpace(acceptLanguage)
	if header == "" {
		return DefaultLang
	}
	first := strings.Split(header, ",")[0]
	first = strings.TrimSpace(strings.Split(first, ";")[0])
	if len(first) < 2 {
		return DefaultLang
	}
	return NormalizeLangCode(first[:2])
}

func T(langCode, key string) string {
	for _, lang := range fallbackLangs(langCode) {
		if val, ok := dict[lang][key]; ok {
			return val
		}
	}
	return key
}

func fallbackLangs(langCode string) []string {
	lang := NormalizeLangCode(langCode)
	switch lang {
	case "TR":
		return []string{"TR"}
	case "EN":
		return []string{"EN", "TR"}
	default:
		return []string{lang, "EN", "TR"}
	}
}

var dict = map[string]map[string]string{
	"TR": {
		"pdf.report_title":      "Cari Hesap Raporu",
		"pdf.date":              "Tarih",
		"pdf.customer":          "Cari",
		"pdf.date_range":        "Tarih Aralığı",
		"pdf.page":              "Sayfa",
		"pdf.ending_balance":    "Son Bakiye",
		"pdf.currency_prefix":   "Para Birimi",
		"pdf.balance_prefix":    "Bakiye",
		"pdf.main.doc_no":       "Belge No",
		"pdf.main.date":         "Tarih",
		"pdf.main.due_date":     "Vade",
		"pdf.main.operation":    "İşlem",
		"pdf.main.description":  "Açıklama",
		"pdf.main.currency":     "Para",
		"pdf.main.debit":        "Borç",
		"pdf.main.credit":       "Alacak",
		"pdf.main.balance":      "Bakiye",
		"pdf.detail.main_group": "Ana Grup",
		"pdf.detail.sub_group":  "Alt Grup",
		"pdf.detail.waiter":     "Garson",
		"pdf.detail.fit":        "Fit",
		"pdf.detail.content":    "İçerik",
		"pdf.detail.product":    "Ürün",
		"pdf.detail.color":      "Renk",
		"pdf.detail.qty":        "Adet",
		"pdf.detail.price":      "Fiyat",
		"pdf.detail.total":      "Tutar",
	},
	"EN": {
		"pdf.report_title":      "Customer Account Report",
		"pdf.date":              "Date",
		"pdf.customer":          "Customer",
		"pdf.date_range":        "Date Range",
		"pdf.page":              "Page",
		"pdf.ending_balance":    "Ending Balance",
		"pdf.currency_prefix":   "Currency",
		"pdf.balance_prefix":    "Balance",
		"pdf.main.doc_no":       "Document No",
		"pdf.main.date":         "Date",
		"pdf.main.due_date":     "Due Date",
		"pdf.main.operation":    "Operation",
		"pdf.main.description":  "Description",
		"pdf.main.currency":     "Curr.",
		"pdf.main.debit":        "Debit",
		"pdf.main.credit":       "Credit",
		"pdf.main.balance":      "Balance",
		"pdf.detail.main_group": "Main Group",
		"pdf.detail.sub_group":  "Sub Group",
		"pdf.detail.waiter":     "Waiter",
		"pdf.detail.fit":        "Fit",
		"pdf.detail.content":    "Content",
		"pdf.detail.product":    "Product",
		"pdf.detail.color":      "Color",
		"pdf.detail.qty":        "Qty",
		"pdf.detail.price":      "Price",
		"pdf.detail.total":      "Total",
	},
}
