package errors

import "fmt"

// 错误码定义
const (
	// 成功
	CodeSuccess = 0

	// 客户端错误 (1000-1999) -> 4xx HTTP 状态码
	CodeInvalidParam    = 1001 // 参数验证失败
	CodeMissingToken    = 1002 // 缺失认证令牌
	CodeInvalidToken    = 1003 // 无效或过期的令牌
	CodeUnauthorized    = 1004 // 未授权
	CodeForbidden       = 1005 // 禁止访问
	CodeNotFound        = 1006 // 资源未找到
	CodeConflict        = 1007 // 资源冲突
	CodeTooManyRequests = 1008 // 请求过多
	CodeRequestTooLarge = 1009 // 请求体过大

	// RBAC 相关错误 (1010-1099)
	CodeAccountNotFound      = 1010 // 账号不存在
	CodeAccountDisabled      = 1011 // 账号已禁用
	CodeAccountDeleted       = 1012 // 账号已删除
	CodeUsernameExists       = 1013 // 用户名已存在
	CodePhoneExists          = 1014 // 手机号已存在
	CodeInvalidPassword      = 1015 // 密码格式不正确
	CodePasswordTooWeak      = 1016 // 密码强度不足
	CodeParentIDRequired     = 1017 // 非 root 用户必须提供上级账号
	CodeInvalidParentID      = 1018 // 上级账号不存在或无效
	CodeCannotModifyParent   = 1019 // 禁止修改上级账号
	CodeCannotModifyUserType = 1020 // 禁止修改用户类型
	CodeRoleNotFound         = 1021 // 角色不存在
	CodeRoleNameExists       = 1022 // 角色名称已存在
	CodePermissionNotFound   = 1023 // 权限不存在
	CodePermCodeExists       = 1024 // 权限编码已存在
	CodeInvalidPermCode      = 1025 // 权限编码格式不正确
	CodeRoleAlreadyAssigned  = 1026 // 角色已分配
	CodePermAlreadyAssigned  = 1027 // 权限已分配
	CodeRoleInUse            = 1028 // 角色使用中，无法删除

	// 认证相关错误 (1040-1049)
	CodeInvalidCredentials    = 1040 // 用户名或密码错误
	CodeAccountLocked         = 1041 // 账号已锁定
	CodePasswordExpired       = 1042 // 密码已过期
	CodeInvalidOldPassword    = 1043 // 旧密码错误
	CodeWechatOAuthFailed     = 1044 // 微信 OAuth 授权失败
	CodeWechatUserInfoFailed  = 1045 // 获取微信用户信息失败
	CodeWechatPayFailed       = 1046 // 微信支付发起失败
	CodeWechatCallbackInvalid = 1047 // 微信回调签名验证失败
	CodeOpenAPIAuthFailed     = 1048 // 开放接口认证失败
	CodeOpenAPISignInvalid    = 1049 // 开放接口签名无效

	// 组织相关错误 (1030-1049)
	CodeShopNotFound         = 1030 // 店铺不存在
	CodeShopCodeExists       = 1031 // 店铺编号已存在
	CodeShopLevelExceeded    = 1032 // 店铺层级超过最大值
	CodeEnterpriseNotFound   = 1033 // 企业不存在
	CodeEnterpriseCodeExists = 1034 // 企业编号已存在
	CodeCustomerNotFound     = 1035 // 个人客户不存在
	CodeCustomerPhoneExists  = 1036 // 个人客户手机号已存在
	CodeShopDisabled         = 1037 // 店铺已禁用

	// 财务相关错误 (1050-1069)
	CodeInvalidStatus       = 1050 // 状态不允许此操作
	CodeInsufficientBalance = 1051 // 余额不足
	CodeWithdrawalNotFound  = 1052 // 提现申请不存在
	CodeWalletNotFound      = 1053 // 钱包不存在
	CodeInsufficientQuota   = 1054 // 额度不足

	// IoT 卡相关错误 (1070-1089)
	CodeIotCardNotFound                   = 1070 // IoT 卡不存在
	CodeIotCardBoundToDevice              = 1071 // IoT 卡已绑定设备
	CodeIotCardStatusNotAllowed           = 1072 // 卡状态不允许此操作
	CodeAssetAllocationRecordNotFound     = 1073 // 分配记录不存在
	CodeNotDirectSubordinate              = 1074 // 非直属下级店铺
	CodeCannotAllocateToSelf              = 1075 // 不能分配给自己
	CodeCannotRecallFromSelf              = 1076 // 不能从自己回收
	CodeCardAlreadyAuthorized             = 1077 // 卡已授权给该企业
	CodeCardNotAuthorized                 = 1078 // 卡未授权给该企业
	CodeCannotAuthorizeOthersCard         = 1079 // 不能授权非自己的卡
	CodeCannotRevokeOthersAuthorization   = 1080 // 不能回收非自己创建的授权
	CodeCannotAuthorizeBoundCard          = 1081 // 不能授权已绑定设备的卡
	CodeCannotAuthorizeToOthersEnterprise = 1082 // 不能授权给非自己的企业
	CodeDeviceAlreadyAuthorized           = 1083 // 设备已授权给该企业
	CodeDeviceNotAuthorized               = 1084 // 设备未授权给该企业
	CodeDeviceAuthorizedToOther           = 1085 // 设备已授权给其他企业
	CodeCannotAuthorizeOthersDevice       = 1086 // 不能授权非自己的设备

	// 对象存储相关错误 (1090-1099)
	CodeStorageNotConfigured   = 1090 // 对象存储服务未配置
	CodeStorageUploadFailed    = 1091 // 文件上传失败
	CodeStorageDownloadFailed  = 1092 // 文件下载失败
	CodeStorageFileNotFound    = 1093 // 文件不存在
	CodeStorageInvalidPurpose  = 1094 // 不支持的文件用途
	CodeStorageInvalidFileType = 1095 // 不支持的文件类型

	// 运营商相关错误 (1100-1109)
	CodeCarrierNotFound   = 1100 // 运营商不存在
	CodeCarrierCodeExists = 1101 // 运营商编码已存在

	// Gateway 相关错误 (1110-1119)
	CodeGatewayError        = 1110 // Gateway 通用错误
	CodeGatewayEncryptError = 1111 // 数据加密失败
	CodeGatewaySignError    = 1112 // 签名生成失败
	CodeGatewayTimeout      = 1113 // 请求超时
	CodeGatewayInvalidResp  = 1114 // 响应格式错误

	// 充值相关错误 (1120-1129)
	CodeRechargeAmountInvalid = 1120 // 充值金额不符合要求
	CodeRechargeNotFound      = 1121 // 充值订单不存在
	CodeRechargeAlreadyPaid   = 1122 // 充值订单已支付

	// 代购相关错误 (1130-1139)
	CodePurchaseOnBehalfForbidden     = 1130 // 无权使用线下支付
	CodePurchaseOnBehalfInvalidTarget = 1131 // 代购目标无效

	// 强充验证相关错误 (1140-1149)
	CodeForceRechargeRequired       = 1140 // 必须充值指定金额
	CodeForceRechargeAmountMismatch = 1141 // 强充金额不匹配

	// 轮询系统相关错误 (1150-1169)
	CodePollingConfigNotFound        = 1150 // 轮询配置不存在
	CodePollingConfigNameExists      = 1151 // 轮询配置名称已存在
	CodePollingQueueFull             = 1152 // 轮询队列已满
	CodePollingConcurrencyLimit      = 1153 // 并发数已达上限
	CodePollingAlertRuleNotFound     = 1154 // 告警规则不存在
	CodePollingCleanupConfigNotFound = 1155 // 数据清理配置不存在
	CodePollingManualTriggerLimit    = 1156 // 手动触发次数已达上限

	// 套餐相关错误 (1160-1169)
	CodeNoAvailablePackage        = 1160 // 没有可用套餐
	CodePackageActivationConflict = 1161 // 套餐正在激活中
	CodeNoMainPackage             = 1162 // 必须有主套餐才能购买加油包
	CodeRealnameRequired          = 1163 // 设备/卡必须先完成实名认证才能购买套餐
	CodeMixedOrderForbidden       = 1164 // 同订单不能同时购买正式套餐和加油包
	CodeMainPackageExists         = 1165 // 已有主套餐，不能重复购买主套餐

	// 微信配置相关错误 (1170-1179)
	CodeWechatConfigNotFound         = 1170 // 微信支付配置不存在
	CodeWechatConfigActive           = 1171 // 不能删除/操作当前生效的支付配置
	CodeWechatConfigHasPendingOrders = 1172 // 该配置存在未完成的支付订单
	CodeFuiouPayFailed               = 1173 // 富友支付发起失败
	CodeFuiouCallbackInvalid         = 1174 // 富友回调签名验证失败
	CodeNoPaymentConfig              = 1175 // 当前无可用的支付配置

	// C端认证相关错误 (1180-1199)
	CodeAssetNotFound           = 1180 // 资产不存在（A1 资产验证失败）
	CodeWechatConfigUnavailable = 1181 // 微信配置不可用（无激活配置）
	CodeSmsSendFailed           = 1182 // 短信发送失败
	CodeVerificationCodeInvalid = 1183 // 验证码错误或已过期
	CodePhoneAlreadyBound       = 1184 // 手机号已被其他客户绑定
	CodeAlreadyBoundPhone       = 1185 // 当前客户已绑定手机号，不可重复绑定
	CodeOldPhoneMismatch        = 1186 // 旧手机号与当前绑定不匹配
	CodeNeedRealname            = 1187 // 该套餐需实名认证后购买
	CodeOpenIDNotFound          = 1188 // 未找到微信授权信息，请先完成授权
	CodeRealnameNotAvailable    = 1189 // 请先完成充值或购买套餐后再进行实名认证
	CodeOpenAPITimestampInvalid = 1190 // 开放接口时间戳无效
	CodeOpenAPINonceReplay      = 1191 // 开放接口 nonce 重放

	// 换货相关错误 (1200-1209)
	CodeExchangeOrderNotFound      = 1200 // 换货单不存在
	CodeExchangeInProgress         = 1201 // 存在进行中的换货单
	CodeExchangeStatusInvalid      = 1202 // 换货单状态不允许此操作
	CodeExchangeAssetTypeMismatch  = 1203 // 换货资产类型必须一致
	CodeExchangeNewAssetNotInStock = 1204 // 新资产非在库状态
	CodeExchangeAssetNotExchanged  = 1205 // 资产未处于已换货状态，不允许转新
	CodeExchangeMigrationFailed    = 1206 // 数据迁移失败
	CodeExchangeActiveRefund       = 1207 // 资产存在未终结退款申请
	CodePaymentMethodUnavailable   = 1208 // 当前资产不支持所选支付方式

	// 企业微信相关错误 (1210-1219)
	CodeWeComApplicationNotFound = 1210 // 企业微信应用配置不存在
	CodeWeComCredentialInvalid   = 1211 // 企业微信加密凭据不可用

	// 服务端错误 (2000-2999) -> 5xx HTTP 状态码
	CodeInternalError      = 2001 // 内部服务器错误
	CodeDatabaseError      = 2002 // 数据库错误
	CodeRedisError         = 2003 // Redis 错误
	CodeServiceUnavailable = 2004 // 服务不可用
	CodeTimeout            = 2005 // 请求超时
	CodeTaskQueueError     = 2006 // 任务队列错误
)

// allErrorCodes 所有已注册的错误码
// 新增错误码时必须同时在此列表中注册
var allErrorCodes = []int{
	CodeSuccess,
	CodeInvalidParam,
	CodeMissingToken,
	CodeInvalidToken,
	CodeUnauthorized,
	CodeForbidden,
	CodeNotFound,
	CodeConflict,
	CodeTooManyRequests,
	CodeRequestTooLarge,
	CodeAccountNotFound,
	CodeAccountDisabled,
	CodeAccountDeleted,
	CodeUsernameExists,
	CodePhoneExists,
	CodeInvalidPassword,
	CodePasswordTooWeak,
	CodeParentIDRequired,
	CodeInvalidParentID,
	CodeCannotModifyParent,
	CodeCannotModifyUserType,
	CodeRoleNotFound,
	CodeRoleNameExists,
	CodePermissionNotFound,
	CodePermCodeExists,
	CodeInvalidPermCode,
	CodeRoleAlreadyAssigned,
	CodePermAlreadyAssigned,
	CodeRoleInUse,
	CodeShopNotFound,
	CodeShopCodeExists,
	CodeShopLevelExceeded,
	CodeEnterpriseNotFound,
	CodeEnterpriseCodeExists,
	CodeCustomerNotFound,
	CodeCustomerPhoneExists,
	CodeShopDisabled,
	CodeInvalidCredentials,
	CodeAccountLocked,
	CodePasswordExpired,
	CodeInvalidOldPassword,
	CodeWechatOAuthFailed,
	CodeWechatUserInfoFailed,
	CodeWechatPayFailed,
	CodeWechatCallbackInvalid,
	CodeOpenAPIAuthFailed,
	CodeOpenAPISignInvalid,
	CodeInvalidStatus,
	CodeInsufficientBalance,
	CodeWithdrawalNotFound,
	CodeWalletNotFound,
	CodeInsufficientQuota,
	CodeIotCardNotFound,
	CodeIotCardBoundToDevice,
	CodeIotCardStatusNotAllowed,
	CodeAssetAllocationRecordNotFound,
	CodeNotDirectSubordinate,
	CodeCannotAllocateToSelf,
	CodeCannotRecallFromSelf,
	CodeCardAlreadyAuthorized,
	CodeCardNotAuthorized,
	CodeCannotAuthorizeOthersCard,
	CodeCannotRevokeOthersAuthorization,
	CodeCannotAuthorizeBoundCard,
	CodeCannotAuthorizeToOthersEnterprise,
	CodeDeviceAlreadyAuthorized,
	CodeDeviceNotAuthorized,
	CodeDeviceAuthorizedToOther,
	CodeCannotAuthorizeOthersDevice,
	CodeStorageNotConfigured,
	CodeStorageUploadFailed,
	CodeStorageDownloadFailed,
	CodeStorageFileNotFound,
	CodeStorageInvalidPurpose,
	CodeStorageInvalidFileType,
	CodeCarrierNotFound,
	CodeCarrierCodeExists,
	CodeGatewayError,
	CodeGatewayEncryptError,
	CodeGatewaySignError,
	CodeGatewayTimeout,
	CodeGatewayInvalidResp,
	CodeRechargeAmountInvalid,
	CodeRechargeNotFound,
	CodeRechargeAlreadyPaid,
	CodePurchaseOnBehalfForbidden,
	CodePurchaseOnBehalfInvalidTarget,
	CodeForceRechargeRequired,
	CodeForceRechargeAmountMismatch,
	CodePollingConfigNotFound,
	CodePollingConfigNameExists,
	CodePollingQueueFull,
	CodePollingConcurrencyLimit,
	CodePollingAlertRuleNotFound,
	CodePollingCleanupConfigNotFound,
	CodePollingManualTriggerLimit,
	CodeNoAvailablePackage,
	CodePackageActivationConflict,
	CodeNoMainPackage,
	CodeRealnameRequired,
	CodeMixedOrderForbidden,
	CodeMainPackageExists,
	CodeWechatConfigNotFound,
	CodeWechatConfigActive,
	CodeWechatConfigHasPendingOrders,
	CodeFuiouPayFailed,
	CodeFuiouCallbackInvalid,
	CodeNoPaymentConfig,
	CodeAssetNotFound,
	CodeWechatConfigUnavailable,
	CodeSmsSendFailed,
	CodeVerificationCodeInvalid,
	CodePhoneAlreadyBound,
	CodeAlreadyBoundPhone,
	CodeOldPhoneMismatch,
	CodeNeedRealname,
	CodeOpenIDNotFound,
	CodeRealnameNotAvailable,
	CodeOpenAPITimestampInvalid,
	CodeOpenAPINonceReplay,
	CodeExchangeOrderNotFound,
	CodeExchangeInProgress,
	CodeExchangeStatusInvalid,
	CodeExchangeAssetTypeMismatch,
	CodeExchangeNewAssetNotInStock,
	CodeExchangeAssetNotExchanged,
	CodeExchangeMigrationFailed,
	CodeExchangeActiveRefund,
	CodePaymentMethodUnavailable,
	CodeWeComApplicationNotFound,
	CodeWeComCredentialInvalid,
	CodeInternalError,
	CodeDatabaseError,
	CodeRedisError,
	CodeServiceUnavailable,
	CodeTimeout,
	CodeTaskQueueError,
}

func init() {
	for _, code := range allErrorCodes {
		if _, ok := errorMessages[code]; !ok {
			panic(fmt.Sprintf("错误码 %d 缺少映射消息，请在 errorMessages 中添加", code))
		}
	}
}

// errorMessages 错误消息映射表（中文）
var errorMessages = map[int]string{
	CodeSuccess:                           "成功",
	CodeInvalidParam:                      "参数验证失败",
	CodeMissingToken:                      "缺失认证令牌",
	CodeInvalidToken:                      "无效或过期的令牌",
	CodeUnauthorized:                      "未授权访问",
	CodeForbidden:                         "禁止访问",
	CodeNotFound:                          "资源未找到",
	CodeConflict:                          "资源冲突",
	CodeTooManyRequests:                   "请求过多,请稍后重试",
	CodeRequestTooLarge:                   "请求体过大",
	CodeAccountNotFound:                   "账号不存在",
	CodeAccountDisabled:                   "账号已禁用",
	CodeAccountDeleted:                    "账号已删除",
	CodeUsernameExists:                    "用户名已存在",
	CodePhoneExists:                       "手机号已存在",
	CodeInvalidPassword:                   "密码格式不正确",
	CodePasswordTooWeak:                   "密码强度不足",
	CodeParentIDRequired:                  "非 root 用户必须提供上级账号",
	CodeInvalidParentID:                   "上级账号不存在或无效",
	CodeCannotModifyParent:                "禁止修改上级账号",
	CodeCannotModifyUserType:              "禁止修改用户类型",
	CodeRoleNotFound:                      "角色不存在",
	CodeRoleNameExists:                    "角色名称已存在",
	CodePermissionNotFound:                "权限不存在",
	CodePermCodeExists:                    "权限编码已存在",
	CodeInvalidPermCode:                   "权限编码格式不正确（应为 module:action 格式）",
	CodeRoleAlreadyAssigned:               "角色已分配",
	CodePermAlreadyAssigned:               "权限已分配",
	CodeRoleInUse:                         "角色使用中，无法删除",
	CodeShopNotFound:                      "店铺不存在",
	CodeShopCodeExists:                    "店铺编号已存在",
	CodeShopLevelExceeded:                 "店铺层级不能超过 7 级",
	CodeEnterpriseNotFound:                "企业不存在",
	CodeEnterpriseCodeExists:              "企业编号已存在",
	CodeCustomerNotFound:                  "个人客户不存在",
	CodeCustomerPhoneExists:               "个人客户手机号已存在",
	CodeShopDisabled:                      "店铺已禁用",
	CodeInvalidStatus:                     "状态不允许此操作",
	CodeInsufficientBalance:               "余额不足",
	CodeWithdrawalNotFound:                "提现申请不存在",
	CodeWalletNotFound:                    "钱包不存在",
	CodeInsufficientQuota:                 "额度不足",
	CodeIotCardNotFound:                   "IoT 卡不存在",
	CodeIotCardBoundToDevice:              "IoT 卡已绑定设备，不能单独操作",
	CodeIotCardStatusNotAllowed:           "卡状态不允许此操作",
	CodeAssetAllocationRecordNotFound:     "分配记录不存在",
	CodeNotDirectSubordinate:              "只能操作直属下级店铺",
	CodeCannotAllocateToSelf:              "不能分配给自己",
	CodeCannotRecallFromSelf:              "不能从自己回收",
	CodeCardAlreadyAuthorized:             "卡已授权给该企业",
	CodeCardNotAuthorized:                 "卡未授权给该企业",
	CodeCannotAuthorizeOthersCard:         "不能授权非自己的卡",
	CodeCannotRevokeOthersAuthorization:   "不能回收非自己创建的授权",
	CodeCannotAuthorizeBoundCard:          "不能授权已绑定设备的卡",
	CodeCannotAuthorizeToOthersEnterprise: "不能授权给非自己的企业",
	CodeDeviceAlreadyAuthorized:           "设备已授权给该企业",
	CodeDeviceNotAuthorized:               "设备未授权给该企业",
	CodeDeviceAuthorizedToOther:           "设备已授权给其他企业",
	CodeCannotAuthorizeOthersDevice:       "不能授权非自己的设备",
	CodeStorageNotConfigured:              "对象存储服务未配置",
	CodeStorageUploadFailed:               "文件上传失败",
	CodeStorageDownloadFailed:             "文件下载失败",
	CodeStorageFileNotFound:               "文件不存在",
	CodeStorageInvalidPurpose:             "不支持的文件用途",
	CodeStorageInvalidFileType:            "不支持的文件类型",
	CodeCarrierNotFound:                   "运营商不存在",
	CodeCarrierCodeExists:                 "运营商编码已存在",
	CodeGatewayError:                      "Gateway 请求失败",
	CodeGatewayEncryptError:               "数据加密失败",
	CodeGatewaySignError:                  "签名生成失败",
	CodeGatewayTimeout:                    "Gateway 请求超时",
	CodeGatewayInvalidResp:                "Gateway 响应格式错误",
	CodeRechargeAmountInvalid:             "充值金额不符合要求",
	CodeRechargeNotFound:                  "充值订单不存在",
	CodeRechargeAlreadyPaid:               "充值订单已支付",
	CodePurchaseOnBehalfForbidden:         "无权使用线下支付",
	CodePurchaseOnBehalfInvalidTarget:     "代购目标无效",
	CodeForceRechargeRequired:             "必须充值指定金额",
	CodeForceRechargeAmountMismatch:       "强充金额不匹配",
	CodePollingConfigNotFound:             "轮询配置不存在",
	CodePollingConfigNameExists:           "轮询配置名称已存在",
	CodePollingQueueFull:                  "轮询队列已满",
	CodePollingConcurrencyLimit:           "并发数已达上限",
	CodePollingAlertRuleNotFound:          "告警规则不存在",
	CodePollingCleanupConfigNotFound:      "数据清理配置不存在",
	CodePollingManualTriggerLimit:         "手动触发次数已达上限",
	CodeNoAvailablePackage:                "没有可用套餐",
	CodePackageActivationConflict:         "套餐正在激活中，请稍后重试",
	CodeNoMainPackage:                     "必须有主套餐才能购买加油包",
	CodeRealnameRequired:                  "设备/卡必须先完成实名认证才能购买套餐",
	CodeMixedOrderForbidden:               "同订单不能同时购买正式套餐和加油包",
	CodeMainPackageExists:                 "已有主套餐，不能重复购买主套餐",
	CodeWechatConfigNotFound:              "微信支付配置不存在",
	CodeWechatConfigActive:                "不能删除当前生效的支付配置，请先停用",
	CodeWechatConfigHasPendingOrders:      "该配置存在未完成的支付订单，暂时无法删除",
	CodeFuiouPayFailed:                    "支付发起失败，请重试",
	CodeFuiouCallbackInvalid:              "支付回调签名验证失败",
	CodeNoPaymentConfig:                   "当前无可用的支付配置，请联系管理员",
	CodeAssetNotFound:                     "资产不存在",
	CodeWechatConfigUnavailable:           "微信配置不可用",
	CodeSmsSendFailed:                     "短信发送失败",
	CodeVerificationCodeInvalid:           "验证码错误或已过期",
	CodePhoneAlreadyBound:                 "手机号已被其他客户绑定",
	CodeAlreadyBoundPhone:                 "当前客户已绑定手机号，不可重复绑定",
	CodeOldPhoneMismatch:                  "旧手机号与当前绑定不匹配",
	CodeNeedRealname:                      "该套餐需实名认证后购买",
	CodeOpenIDNotFound:                    "未找到微信授权信息，请先完成授权",
	CodeRealnameNotAvailable:              "请先完成充值或购买套餐后再进行实名认证",
	CodeExchangeOrderNotFound:             "换货单不存在或无权限",
	CodeExchangeInProgress:                "该资产存在进行中的换货单",
	CodeExchangeStatusInvalid:             "换货单当前状态不允许此操作",
	CodeExchangeAssetTypeMismatch:         "换货资产类型必须一致（卡换卡/设备换设备）",
	CodeExchangeNewAssetNotInStock:        "新资产非在库状态，不可用于换货",
	CodeExchangeAssetNotExchanged:         "资产当前状态不允许转新",
	CodeExchangeMigrationFailed:           "换货数据迁移失败",
	CodeExchangeActiveRefund:              "该资产存在退款申请",
	CodePaymentMethodUnavailable:          "当前资产不支持所选支付方式",
	CodeWeComApplicationNotFound:          "企业微信应用配置不存在或已禁用",
	CodeWeComCredentialInvalid:            "企业微信凭据配置无效",
	CodeInvalidCredentials:                "用户名或密码错误",
	CodeAccountLocked:                     "账号已锁定",
	CodePasswordExpired:                   "密码已过期",
	CodeInvalidOldPassword:                "旧密码错误",
	CodeWechatOAuthFailed:                 "微信授权失败",
	CodeWechatUserInfoFailed:              "获取微信用户信息失败",
	CodeWechatPayFailed:                   "微信支付发起失败",
	CodeWechatCallbackInvalid:             "微信回调验证失败",
	CodeOpenAPIAuthFailed:                 "开放接口认证失败",
	CodeOpenAPISignInvalid:                "开放接口签名无效",
	CodeOpenAPITimestampInvalid:           "开放接口时间戳无效",
	CodeOpenAPINonceReplay:                "重复请求，请勿重放",
	CodeInternalError:                     "内部服务器错误",
	CodeDatabaseError:                     "数据库错误",
	CodeRedisError:                        "缓存服务错误",
	CodeServiceUnavailable:                "服务暂时不可用",
	CodeTimeout:                           "请求超时",
	CodeTaskQueueError:                    "任务队列错误",
}

// GetMessage 获取错误码对应的消息
// lang 参数暂时保留以便未来支持多语言，目前仅支持中文
func GetMessage(code int, lang string) string {
	if msg, ok := errorMessages[code]; ok {
		return msg
	}
	// 未定义的错误码返回默认消息
	if code >= 2000 && code < 3000 {
		return "内部服务器错误"
	}
	return "请求处理失败"
}

// GetHTTPStatus 将错误码映射为 HTTP 状态码
func GetHTTPStatus(code int) int {
	switch code {
	case CodeSuccess:
		return 200 // OK
	case CodeInvalidParam, CodeRequestTooLarge:
		return 400 // Bad Request
	case CodeMissingToken, CodeInvalidToken, CodeUnauthorized, CodeOpenAPIAuthFailed, CodeOpenAPISignInvalid, CodeOpenAPITimestampInvalid:
		return 401 // Unauthorized
	case CodeForbidden:
		return 403 // Forbidden
	case CodeNotFound:
		return 404 // Not Found
	case CodeConflict,
		CodeUsernameExists,
		CodePhoneExists,
		CodeRoleNameExists,
		CodePermCodeExists,
		CodeShopCodeExists,
		CodeEnterpriseCodeExists,
		CodeCustomerPhoneExists,
		CodeCarrierCodeExists,
		CodeOpenAPINonceReplay:
		return 409 // Conflict
	case CodeTooManyRequests:
		return 429 // Too Many Requests
	case CodeServiceUnavailable:
		return 503 // Service Unavailable
	case CodeTimeout:
		return 504 // Gateway Timeout
	default:
		// 服务端错误（2000-2999）默认映射为 500
		if code >= 2000 && code < 3000 {
			return 500 // Internal Server Error
		}
		// 客户端错误（1000-1999）默认映射为 400
		if code >= 1000 && code < 2000 {
			return 400 // Bad Request
		}
		// 其他未知错误默认为 500
		return 500 // Internal Server Error
	}
}

// GetLogLevel 将错误码映射为日志级别
// 返回值: "warn" (客户端错误), "error" (服务端错误), "info" (成功)
func GetLogLevel(code int) string {
	if code == 0 {
		return "info" // 成功
	}
	if code >= 2000 && code < 3000 {
		return "error" // 服务端错误
	}
	if code >= 1000 && code < 2000 {
		return "warn" // 客户端错误
	}
	return "error" // 默认为错误级别
}
