package resource

import (
	"strings"
	"unicode/utf8"
)

type GiftConfig struct {
	AppCode          string
	GiftID           string
	ResourceID       int64
	Resource         Resource
	Status           string
	Name             string
	SortOrder        int32
	PresentationJSON string
	PriceVersion     string
	CoinPrice        int64
	// GiftPointAmount 是历史字段；送礼结算不再读取，新配置固定为 0。
	GiftPointAmount int64
	HeatValue       int64
	GiftTypeCode    string
	// VIPLevel 是 VIP/贵族礼物对应的 VIP 等级；普通礼物固定为 0。
	VIPLevel int32
	// CPRelationType 只在 gift_type_code=cp 的礼物上使用，值为 cp、brother 或 sister。
	CPRelationType  string
	ChargeAssetType string
	EffectiveFromMS int64
	EffectiveToMS   int64
	EffectTypes     []string
	CreatedByUserID int64
	UpdatedByUserID int64
	CreatedAtMS     int64
	UpdatedAtMS     int64
	RegionIDs       []int64
}

type GiftTypeConfig struct {
	AppCode         string
	TypeCode        string
	Name            string
	TabKey          string
	Status          string
	SortOrder       int32
	CreatedByUserID int64
	UpdatedByUserID int64
	CreatedAtMS     int64
	UpdatedAtMS     int64
}

type ListGiftConfigsQuery struct {
	AppCode      string
	Status       string
	Keyword      string
	GiftTypeCode string
	Page         int32
	PageSize     int32
	ActiveOnly   bool
	RegionID     int64
	FilterRegion bool
}

type ListGiftTypeConfigsQuery struct {
	AppCode    string
	Status     string
	ActiveOnly bool
}

type GiftConfigCommand struct {
	AppCode          string
	GiftID           string
	ResourceID       int64
	Status           string
	Name             string
	SortOrder        int32
	PresentationJSON string
	PriceVersion     string
	CoinPrice        int64
	// GiftPointAmount 是历史字段；保存礼物价格时固定为 0，业务计算只读 CoinPrice。
	GiftPointAmount int64
	HeatValue       int64
	EffectiveAtMS   int64
	GiftTypeCode    string
	// VIPLevel 使用独立列保存，不能藏进 presentation_json 造成服务端无法校验。
	VIPLevel int32
	// CPRelationType 会写入 presentation_json，避免新增 gift_configs 表字段。
	CPRelationType  string
	ChargeAssetType string
	EffectiveFromMS int64
	EffectiveToMS   int64
	EffectTypes     []string
	OperatorUserID  int64
	RegionIDs       []int64
}

type GiftTypeConfigCommand struct {
	AppCode        string
	TypeCode       string
	Name           string
	TabKey         string
	Status         string
	SortOrder      int32
	OperatorUserID int64
}

func NormalizeGiftTypeCode(value string) string {
	value = strings.ToLower(strings.TrimSpace(value))
	value = strings.ReplaceAll(value, "-", "_")
	if value == "" {
		return GiftTypeNormal
	}
	return value
}

func ValidGiftTypeCode(value string) bool {
	value = NormalizeGiftTypeCode(value)
	if value == "" || len(value) > 32 {
		return false
	}
	for index, char := range value {
		if index == 0 && (char < 'a' || char > 'z') {
			return false
		}
		if (char >= 'a' && char <= 'z') || (char >= '0' && char <= '9') || char == '_' {
			continue
		}
		return false
	}
	return true
}

func NormalizeGiftTypeTabKey(value string) string {
	return strings.TrimSpace(value)
}

func ValidGiftTypeTabKey(value string) bool {
	value = NormalizeGiftTypeTabKey(value)
	return value != "" && utf8.RuneCountInString(value) <= 32
}

func DefaultGiftTypeConfigs(appCode string) []GiftTypeConfig {
	appCode = strings.TrimSpace(appCode)
	defaults := []GiftTypeConfig{
		{TypeCode: GiftTypeNormal, Name: "普通礼物", TabKey: GiftTypeTabKeyNormal, Status: StatusActive, SortOrder: 10},
		{TypeCode: GiftTypeCP, Name: "CP礼物", TabKey: GiftTypeTabKeyCP, Status: StatusActive, SortOrder: 20},
		{TypeCode: GiftTypeLucky, Name: "幸运礼物", TabKey: GiftTypeTabKeyLucky, Status: StatusActive, SortOrder: 30},
		{TypeCode: GiftTypeSuperLucky, Name: "超级幸运礼物", TabKey: GiftTypeTabKeySuperLucky, Status: StatusActive, SortOrder: 40},
		{TypeCode: GiftTypeExclusive, Name: "专属礼物", TabKey: GiftTypeTabKeyExclusive, Status: StatusActive, SortOrder: 50},
		{TypeCode: GiftTypeNoble, Name: "贵族礼物", TabKey: GiftTypeTabKeyNoble, Status: StatusActive, SortOrder: 60},
		{TypeCode: GiftTypeFlag, Name: "国旗礼物", TabKey: GiftTypeTabKeyFlag, Status: StatusActive, SortOrder: 70},
		{TypeCode: GiftTypeActivity, Name: "活动礼物", TabKey: GiftTypeTabKeyActivity, Status: StatusActive, SortOrder: 80},
		{TypeCode: GiftTypeMagic, Name: "魔法礼物", TabKey: GiftTypeTabKeyMagic, Status: StatusActive, SortOrder: 90},
		{TypeCode: GiftTypeCustom, Name: "定制礼物", TabKey: GiftTypeTabKeyCustom, Status: StatusActive, SortOrder: 100},
	}
	for index := range defaults {
		defaults[index].AppCode = appCode
	}
	return defaults
}

func NormalizeGiftEffectType(value string) string {
	return strings.ToLower(strings.TrimSpace(value))
}

func ValidGiftEffectType(value string) bool {
	switch NormalizeGiftEffectType(value) {
	case GiftEffectAnimation, GiftEffectMusic, GiftEffectGlobalBroadcast:
		return true
	default:
		return false
	}
}
