/**
 * 多平台账户凭据管理系统 - 常量定义
 */

import { ErrorCode, PlatformType, SignatureAlgorithm } from './credential-types'

/**
 * 支持的平台列表
 */
export const SUPPORTED_PLATFORMS: PlatformType[] = ['pacifica', 'aster', 'binance', 'okx']

/**
 * 平台签名算法映射
 */
export const PLATFORM_ALGORITHMS: Record<PlatformType, SignatureAlgorithm> = {
  pacifica: 'ed25519',
  aster: 'eip191',
  binance: 'hmac-sha256',
  okx: 'hmac-sha256',
}

/**
 * 性能要求常量
 */
export const PERFORMANCE_LIMITS = {
  // 签名延迟目标
  MAX_SIGNATURE_TIME_MS: 50,
  MAX_BATCH_SIGNATURE_TIME_MS: 200,
  MAX_CONFIG_RELOAD_TIME_MS: 5000,

  // 吞吐量目标
  MAX_QPS: 100,
  MAX_BATCH_SIZE: 10,

  // 缓存设置
  CONFIG_CACHE_TTL_MS: 120 * 1000, // 2分钟
} as const

/**
 * 配置验证规则
 */
export const VALIDATION_RULES = {
  // 账户ID规则
  ACCOUNT_ID: {
    MIN_LENGTH: 3,
    MAX_LENGTH: 50,
    PATTERN: /^[a-zA-Z0-9\-_]+$/,
  },

  // Pacifica私钥规则
  PACIFICA_PRIVATE_KEY: {
    LENGTH: 44, // base58编码长度
    PATTERN: /^[1-9A-HJ-NP-Za-km-z]{44}$/, // base58字符集
  },

  // 以太坊私钥规则
  ETH_PRIVATE_KEY: {
    LENGTH: 66, // 0x + 64位十六进制
    PATTERN: /^0x[a-fA-F0-9]{64}$/,
  },

  // API Key规则
  API_KEY: {
    MIN_LENGTH: 8,
    MAX_LENGTH: 128,
  },
} as const

/**
 * 错误消息映射
 */
export const ERROR_MESSAGES: Record<ErrorCode, string> = {
  [ErrorCode.ACCOUNT_NOT_FOUND]: '指定账户不存在',
  [ErrorCode.PLATFORM_NOT_SUPPORTED]: '不支持的平台',
  [ErrorCode.INVALID_DATA_FORMAT]: '签名数据格式错误',
  [ErrorCode.SIGNATURE_FAILED]: '签名操作失败',
  [ErrorCode.CONFIG_LOAD_FAILED]: '配置文件加载失败',
  [ErrorCode.ACCOUNT_INACTIVE]: '账户已禁用',
}

/**
 * HTTP状态码映射
 */
export const HTTP_STATUS_CODES: Record<ErrorCode, number> = {
  [ErrorCode.ACCOUNT_NOT_FOUND]: 404,
  [ErrorCode.PLATFORM_NOT_SUPPORTED]: 400,
  [ErrorCode.INVALID_DATA_FORMAT]: 400,
  [ErrorCode.SIGNATURE_FAILED]: 500,
  [ErrorCode.CONFIG_LOAD_FAILED]: 500,
  [ErrorCode.ACCOUNT_INACTIVE]: 423,
}

/**
 * 配置文件默认设置
 */
export const DEFAULT_CONFIG = {
  // 配置文件路径
  DEFAULT_CONFIG_PATH: './credentials.json',

  // 文件监控设置
  WATCH_DEBOUNCE_MS: 1000, // 防抖时间

  // 日志设置
  LOG_LEVEL: 'info',
  AUDIT_LOG_RETENTION_DAYS: 30,

  // 安全设置
  ENCRYPTION_ALGORITHM: 'aes-256-gcm',
  KEY_DERIVATION_ITERATIONS: 100000,
} as const

/**
 * API 默认端口和路径
 */
export const API_CONFIG = {
  DEFAULT_PORT: 3000,
  BASE_PATH: '/api/v1',
  MAX_REQUEST_SIZE: '1mb',
  REQUEST_TIMEOUT_MS: 30000,
} as const

/**
 * Pacifica 平台特定常量
 */
export const PACIFICA_CONFIG = {
  // Ed25519 签名常量
  PRIVATE_KEY_LENGTH: 32, // 字节
  PUBLIC_KEY_LENGTH: 32, // 字节
  SIGNATURE_LENGTH: 64, // 字节

  // Base58 编码长度
  BASE58_PRIVATE_KEY_LENGTH: 44,
  BASE58_PUBLIC_KEY_LENGTH: 44,
  BASE58_SIGNATURE_LENGTH: 88,

  // 支持的订单类型
  SUPPORTED_ORDER_TYPES: ['place_order', 'cancel_order', 'modify_order', 'close_position'],
} as const

/**
 * 日志格式模板
 */
export const LOG_TEMPLATES = {
  SIGNATURE_REQUEST: 'Signature request: platform={platform}, account={account}, type={type}',
  SIGNATURE_SUCCESS: 'Signature completed: platform={platform}, account={account}, time={timeMs}ms',
  SIGNATURE_ERROR: 'Signature failed: platform={platform}, account={account}, error={error}',
  CONFIG_RELOAD: 'Config reloaded: path={path}, accounts={accountCount}, time={timeMs}ms',
  ACCOUNT_ACCESS: 'Account accessed: platform={platform}, account={account}, operation={operation}',
} as const
