package vo

import (
	"time"
	"voucher/internal/pkg/helper"
)

type CacheKey string

const (
	CmbOrderLockKey        CacheKey = "cmb_order"
	CmbQueryLockKey        CacheKey = "cmb_query"
	CmbProductQueryLockKey CacheKey = "cmb_product_query"
	CmbBatchNoticeCacheKey CacheKey = "cmb_batch_notice"
	CmbBatchNoticeLockKey  CacheKey = "cmb_batch_notice_lock"

	NotifyRetryConsume CacheKey = "notify_retry_consume"

	OrderConsumeFailAlarmKey     CacheKey = "order_consume_fail_alarm"
	OrderConsumeFailAlarmLockKey CacheKey = "order_consume_fail_alarm_lock"

	WechatNotifyRegisterTagCacheLockKey CacheKey = "register_tag_lock"

	WechatNotifyConsumeLockKey CacheKey = "wechat_notify_consume"
)

const (
	ProductQueryKey     CacheKey = "product_query"
	ProductQueryLockKey CacheKey = "product_query_lock"
)

const (
	MultiNotifyLockKey CacheKey = "multi_notify_lock_key"
)

var (
	WarningBudgetCron     CacheKey = "warning_budget_cron"
	WarningBudgetSendIncr CacheKey = "warning_budget_incr"
)

var CacheKeyMap = map[CacheKey]time.Duration{
	CmbOrderLockKey:                     30 * time.Second,
	CmbQueryLockKey:                     30 * time.Second,
	CmbProductQueryLockKey:              30 * time.Second,
	CmbBatchNoticeCacheKey:              21600 * time.Second, // 6小时
	CmbBatchNoticeLockKey:               300 * time.Second,
	OrderConsumeFailAlarmKey:            3 * time.Hour, // 3小时
	OrderConsumeFailAlarmLockKey:        60 * time.Second,
	NotifyRetryConsume:                  60 * time.Second,
	WechatNotifyRegisterTagCacheLockKey: 60 * time.Second,
	WechatNotifyConsumeLockKey:          30 * time.Second,

	ProductQueryKey:     30 * 86400 * time.Second, // 30天
	ProductQueryLockKey: 30 * time.Second,

	WarningBudgetSendIncr: 3 * time.Hour,
	WarningBudgetCron:     5 * time.Minute,

	MultiNotifyLockKey: 30 * time.Second,
}

type Cache struct {
	Key string
	TTL time.Duration
}

func (s CacheKey) GetValue() string {
	return string(s)
}

func (s CacheKey) BuildCache(strArr []string) *Cache {

	k := s.GetValue()

	if len(strArr) > 0 {
		k = helper.BuildStr(k, strArr)
	}

	c := &Cache{
		Key: k,
	}

	ttl, ok := CacheKeyMap[s]
	if !ok {
		c.TTL = 30 // 默认30秒
	}

	c.TTL = ttl

	return c
}

func (s CacheKey) BuildCacheUint64(ids []uint64) *Cache {

	k := s.GetValue()

	if len(ids) > 0 {
		k = helper.BuildStr(k, ids)
	}

	c := &Cache{
		Key: k,
	}

	ttl, ok := CacheKeyMap[s]
	if !ok {
		c.TTL = 30 // 默认30秒
	}

	c.TTL = ttl

	return c
}
