// 基础设施模块的类型定义

/**
 * 钱包配置
 */
export interface WalletConfig {
  type: 'hd' | 'private_key' | 'mnemonic' | 'hardware'
  chainId: number
  rpcUrl: string
  gasPrice?: number
  gasLimit?: number

  // HD 钱包配置
  mnemonic?: string
  derivationPath?: string
  accountIndex?: number

  // 私钥配置
  privateKey?: string

  // 硬件钱包配置
  hardwareType?: 'ledger' | 'trezor'
  hardwarePath?: string
}

/**
 * 账户信息
 */
export interface AccountInfo {
  address: string
  chainId: number
  balance: Record<string, number> // token -> balance
  nonce: number
  gasPrice: number

  // 合约地址
  contracts: {
    perpContract?: string
    spotRouter?: string
    oracle?: string
  }

  // 权限
  permissions: {
    canTrade: boolean
    canWithdraw: boolean
    canManage: boolean
  }
}

/**
 * 交易配置
 */
export interface TransactionConfig {
  gasPrice: number
  gasLimit: number
  maxSlippage: number
  deadline: number
  approveAmount?: number
  useFlashbots?: boolean
}

/**
 * 系统配置
 */
export interface SystemConfig {
  // 网络配置
  networks: Record<
    string,
    {
      chainId: number
      rpcUrl: string
      wsUrl?: string
      explorerUrl: string
      gasPrice: number
    }
  >

  // API 配置
  apis: {
    binance?: {
      apiKey: string
      secret: string
      testnet: boolean
    }
    coingecko?: {
      apiKey: string
    }
    chainlink?: {
      feeds: Record<string, string> // symbol -> feed address
    }
  }

  // 数据库配置
  database: {
    host: string
    port: number
    database: string
    username: string
    password: string
    dialect: 'postgres' | 'mysql' | 'sqlite'
    logging: boolean
    pool: {
      max: number
      min: number
      acquire: number
      idle: number
    }
  }

  // Redis 配置
  redis: {
    host: string
    port: number
    password?: string
    db: number
    keyPrefix: string
  }

  // 风险配置
  risk: {
    maxLeverage: number
    maxPositionSize: number
    maxDrawdown: number
    minMarginRatio: number
    maxSlippage: number
  }

  // 交易配置
  trading: {
    defaultSlippage: number
    minOrderValue: number
    maxOrderValue: number
    rebalanceInterval: number
    hedgeThreshold: number
  }

  // 监控配置
  monitoring: {
    alertCooldown: number
    metricsInterval: number
    healthCheckInterval: number
    logLevel: 'debug' | 'info' | 'warn' | 'error'
  }
}

/**
 * 数据库模型定义
 */
export interface DatabaseSchema {
  // 交易表
  trades: {
    id: string
    symbol: string
    side: 'buy' | 'sell'
    type: string
    quantity: number
    price: number
    fee: number
    feeAsset: string
    status: string
    txHash?: string
    timestamp: Date
    metadata?: Record<string, any>
  }

  // 持仓表
  positions: {
    id: string
    symbol: string
    side: 'long' | 'short'
    size: number
    entryPrice: number
    leverage: number
    margin: number
    unrealizedPnL: number
    realizedPnL: number
    liquidationPrice: number
    status: 'open' | 'closed' | 'liquidated'
    openedAt: Date
    closedAt?: Date
    metadata?: Record<string, any>
  }

  // 订单表
  orders: {
    id: string
    symbol: string
    side: 'buy' | 'sell'
    type: string
    quantity: number
    price?: number
    status: string
    filledQuantity: number
    remainingQuantity: number
    fee?: number
    txHash?: string
    createdAt: Date
    updatedAt: Date
  }

  // 警报表
  alerts: {
    id: string
    type: string
    severity: string
    title: string
    message: string
    value: number
    threshold: number
    resolved: boolean
    resolvedAt?: Date
    createdAt: Date
  }

  // 性能指标表
  metrics: {
    id: string
    name: string
    value: number
    labels?: Record<string, string>
    timestamp: Date
  }
}

/**
 * 任务调度配置
 */
export interface JobConfig {
  name: string
  schedule: string // cron 表达式
  enabled: boolean
  retryCount: number
  retryDelay: number
  timeout: number
  params?: Record<string, any>
}

/**
 * 日志配置
 */
export interface LogConfig {
  level: 'debug' | 'info' | 'warn' | 'error'
  format: 'json' | 'simple' | 'detailed'
  transports: {
    console?: {
      enabled: boolean
      level: string
      format: string
    }
    file?: {
      enabled: boolean
      level: string
      filename: string
      maxsize: number
      maxFiles: number
    }
    telegram?: {
      enabled: boolean
      botToken: string
      chatId: string
      level: string
    }
  }
}

/**
 * 通知配置
 */
export interface NotificationConfig {
  telegram: {
    botToken: string
    chatId: string
    enabled: boolean
    templates: Record<string, string>
  }

  email: {
    host: string
    port: number
    secure: boolean
    auth: {
      user: string
      pass: string
    }
    from: string
    to: string[]
    enabled: boolean
    templates: Record<string, string>
  }

  slack: {
    webhook: string
    channel: string
    enabled: boolean
    templates: Record<string, string>
  }
}

/**
 * 健康检查配置
 */
export interface HealthCheckConfig {
  enabled: boolean
  interval: number // 检查间隔（秒）
  timeout: number // 超时时间（秒）

  checks: {
    database?: {
      enabled: boolean
      timeout: number
    }
    redis?: {
      enabled: boolean
      timeout: number
    }
    web3?: {
      enabled: boolean
      timeout: number
      networks: number[]
    }
    exchange?: {
      enabled: boolean
      timeout: number
      apis: string[]
    }
  }

  alerts: {
    enabled: boolean
    thresholds: {
      consecutiveFailures: number
      uptime: number
    }
  }
}

/**
 * 备份配置
 */
export interface BackupConfig {
  enabled: boolean
  schedule: string // cron 表达式
  retention: number // 保留天数

  targets: {
    database?: {
      enabled: boolean
      type: 'full' | 'incremental'
      compression: boolean
    }
    logs?: {
      enabled: boolean
      compression: boolean
    }
    config?: {
      enabled: boolean
    }
  }

  storage: {
    type: 'local' | 's3' | 'ftp'
    path?: string
    bucket?: string
    credentials?: Record<string, string>
  }
}
