package apple

// ==================== API 错误码枚举 ====================
// 统一的REST API错误码，包含标准错误码和Apple特定错误码

type Code int

const (
	// ==================== 成功 (0) ====================
	CodeSuccess Code = 0 // 成功

	// ==================== 认证与授权错误 (1000-1999) ====================
	CodeLoginFailed             Code = 1001 // 登录失败
	CodeTokenExpired            Code = 1002 // token过期
	CodeInsufficientPermissions Code = 1003 // 权限不足
	CodeInvalidToken            Code = 1004 // token无效

	// ==================== 业务逻辑错误 (2000-2999) ====================
	CodeOrderCreationFailed Code = 2001 // 订单创建失败
	CodePaymentFailed       Code = 2002 // 支付失败
	CodeInsufficientBalance Code = 2003 // 余额不足
	CodeOperationNotAllowed Code = 2004 // 操作不允许

	// ==================== 验证错误 (3000-3999) ====================
	CodeInvalidInput         Code = 3001 // 输入无效
	CodeMissingRequiredField Code = 3002 // 缺失必填字段
	CodeInvalidFormat        Code = 3003 // 格式无效
	CodeValidationError      Code = 3004 // 验证错误
	CodeProxyRequestFailed   Code = 3005 // 代理请求失败

	// ==================== 资源错误 (4000-4999) ====================
	CodeResourceNotFound      Code = 4001 // 资源不存在
	CodeResourceAlreadyExists Code = 4002 // 资源已存在
	CodeResourceConflict      Code = 4003 // 资源冲突

	// ==================== 系统错误 (5000-5999) ====================
	CodeDatabaseError        Code = 5001 // 数据库错误
	CodeExternalServiceError Code = 5002 // 外部服务错误
	CodeCacheError           Code = 5003 // 缓存错误
	CodeInternalError        Code = 5004 // 内部系统错误
	CodeNotImplemented       Code = 5005 // 功能未实现

	// ==================== Apple 特定错误 (8000-8099) ====================
	// Apple 账户错误 (8001-8005)
	CodeAppleAccountForbidden       Code = 8001 // 苹果账户被禁用
	CodeAppleAccountLocked          Code = 8002 // 苹果账户被封锁
	CodeApplePasswordError          Code = 8003 // 苹果账号密码错误
	CodeAppleAccountCountryMismatch Code = 8004 // 账户兑换国籍不匹配
	CodeApplePasswordExpired        Code = 8005 // 苹果账号密码失效，需要重新登录
	CodeAppleAccountNotMainland     Code = 8006 // 苹果账户非中国

	// Apple 充值限制错误 (8010-8012)
	CodeAppleRedeemLimitExceeded            Code = 8010 // 充值次数限制
	CodeAppleRedeemLimitExceededTemporarily Code = 8011 // 充值次数临时限制
	CodeAppleRedeemLimitExceededPerMinute   Code = 8012 // 充值次数一分钟限制

	// Apple 卡密错误 (8013-8015)
	CodeAppleRedeemAlreadyClaimed  Code = 8013 // 卡密已经被兑换
	CodeAppleRedeemNotFound        Code = 8014 // 卡密不存在
	CodeAppleRedeemCountryMismatch Code = 8015 // 兑换卡密国籍问题

	// ==================== 未知错误 (9000-9999) ====================
	CodeUnknownError Code = 9000 // 未知错误
)

// String 返回错误码的文字描述
func (c Code) String() string {
	switch c {
	case CodeSuccess:
		return "成功"
	case CodeLoginFailed:
		return "登录失败"
	case CodeTokenExpired:
		return "token过期"
	case CodeInsufficientPermissions:
		return "权限不足"
	case CodeInvalidToken:
		return "token无效"
	case CodeOrderCreationFailed:
		return "订单创建失败"
	case CodePaymentFailed:
		return "支付失败"
	case CodeInsufficientBalance:
		return "余额不足"
	case CodeOperationNotAllowed:
		return "操作不允许"
	case CodeInvalidInput:
		return "输入无效"
	case CodeMissingRequiredField:
		return "缺失必填字段"
	case CodeInvalidFormat:
		return "格式无效"
	case CodeValidationError:
		return "验证错误"
	case CodeResourceNotFound:
		return "资源不存在"
	case CodeResourceAlreadyExists:
		return "资源已存在"
	case CodeResourceConflict:
		return "资源冲突"
	case CodeDatabaseError:
		return "数据库错误"
	case CodeExternalServiceError:
		return "外部服务错误"
	case CodeCacheError:
		return "缓存错误"
	case CodeInternalError:
		return "内部系统错误"
	case CodeNotImplemented:
		return "功能未实现"
	case CodeAppleAccountForbidden:
		return "苹果账户被禁用"
	case CodeAppleAccountLocked:
		return "苹果账户被封锁"
	case CodeApplePasswordError:
		return "苹果账号密码错误"
	case CodeAppleAccountCountryMismatch:
		return "iTunes商店无效"
	case CodeApplePasswordExpired:
		return "苹果账号密码失效，需要重新登录"
	case CodeAppleAccountNotMainland:
		return "苹果账户非国区账户"
	case CodeAppleRedeemLimitExceeded:
		return "充值次数限制"
	case CodeAppleRedeemLimitExceededTemporarily:
		return "充值次数临时限制"
	case CodeAppleRedeemLimitExceededPerMinute:
		return "充值次数一分钟限制"
	case CodeAppleRedeemAlreadyClaimed:
		return "卡密之前已被使用"
	case CodeAppleRedeemNotFound:
		return "卡密不存在"
	case CodeAppleRedeemCountryMismatch:
		return "iTunes商店无效"
	case CodeProxyRequestFailed:
		return "代理请求失败"
	case CodeUnknownError:
		return "未知错误"
	default:
		return "未知错误"
	}
}

// IsSuccess 判断是否成功
func (c Code) IsSuccess() bool {
	return c == CodeSuccess
}

// IsAppleAccountError 判断是否是苹果账户相关错误
func (c Code) IsAppleAccountError() bool {
	return c >= 8001 && c <= 8005
}

// IsAppleRedeemLimitError 判断是否是苹果充值限制错误
func (c Code) IsAppleRedeemLimitError() bool {
	return c >= 8010 && c <= 8012
}

// IsAppleCardError 判断是否是苹果卡密相关错误
func (c Code) IsAppleCardError() bool {
	return c >= 8013 && c <= 8015
}

// IsAppleError 判断是否是苹果特定错误
func (c Code) IsAppleError() bool {
	return c >= 8001 && c <= 8099
}
