package model

import (
	"time"
)

// LimiterType 限流器类型
type LimiterType string

const (
	// LimiterTypeSysUserLogin 用户登录限流
	LimiterTypeSysUserLogin LimiterType = "sys_user:login"
	// LimiterTypeCardInfoJdAccountCookieChecker JD账户Cookie检查限流
	LimiterTypeCardInfoJdAccountCookieChecker LimiterType = "card_info:jd:account:cookie"
	// LimiterTypeCardInfoJdAccountCookieSet JD账户Cookie设置限流
	LimiterTypeCardInfoJdAccountCookieSet LimiterType = "card_info:jd:account:cookie:set"
	// LimiterTypeCardInfoRedeemAccountCookieChecker 通用账户Cookie检查限流
	LimiterTypeCardInfoRedeemAccountCookieChecker LimiterType = "card_info:redeem:account:cookie:checker"
	// LimiterTypeCardInfoRedeemAccountCookieSet 通用账户Cookie设置限流
	LimiterTypeCardInfoRedeemAccountCookieSet LimiterType = "card_info:redeem:account:cookie:set"
)

// LimiterStrategy 限流策略
type LimiterStrategy string

const (
	// StrategySlidingWindow 滑动窗口限流
	StrategySlidingWindow LimiterStrategy = "sliding_window"
	// StrategyFixedWindow 固定窗口限流
	StrategyFixedWindow LimiterStrategy = "fixed_window"
	// StrategyTokenBucket 令牌桶限流
	StrategyTokenBucket LimiterStrategy = "token_bucket"
)

// LimiterConfig 限流器配置
type LimiterConfig struct {
	Type     LimiterType     `json:"type"`     // 限流器类型
	Strategy LimiterStrategy `json:"strategy"` // 限流策略
	Capacity int             `json:"capacity"` // 容量（最大请求数）
	Window   time.Duration   `json:"window"`   // 时间窗口
	Rate     float64         `json:"rate"`     // 速率（用于令牌桶）
}

// LimiterResult 限流检查结果
type LimiterResult struct {
	Allowed    bool          `json:"allowed"`     // 是否允许通过
	Remaining  int           `json:"remaining"`   // 剩余可用次数
	ResetAt    time.Time     `json:"reset_at"`    // 重置时间
	RetryAfter time.Duration `json:"retry_after"` // 建议重试间隔
}

// Key 暴露Key方法，用于生成缓存键
func (lt LimiterType) Key(name interface{}) string {
	return string(lt) + ":" + string(name.(string))
}
