/**
 * Pacifica DEX API Types
 *
 * 定义Pacifica DEX的数据结构和接口类型
 */

// ======================== 基础类型 ========================

export interface PacificaSymbol {
  symbol: string
  baseAsset: string
  quoteAsset: string
  status: 'TRADING' | 'BREAK' | 'AUCTION_MATCH' | 'HALT'
  baseAssetPrecision: number
  quotePrecision: number
  baseCommissionPrecision: number
  quoteCommissionPrecision: number
  orderTypes: string[]
  icebergAllowed: boolean
  ocoAllowed: boolean
  quoteOrderQtyMarketAllowed: boolean
  allowTrailingStop: boolean
  cancelReplaceAllowed: boolean
  isSpotTradingAllowed: boolean
  isMarginTradingAllowed: boolean
  filters: any[]
  permissions: string[]
  defaultSelfTradePreventionMode: string
  allowedSelfTradePreventionModes: string[]
}

export interface PacificaBalance {
  asset: string
  free: string
  locked: string
}

export interface PacificaPosition {
  symbol: string
  positionAmt: string
  entryPrice: string
  breakEvenPrice: string
  markPrice: string
  unRealizedProfit: string
  liquidationPrice: string
  leverage: string
  maxNotionalValue: string
  marginType: 'isolated' | 'cross'
  isolatedMargin: string
  isAutoAddMargin: string
  positionSide: 'BOTH' | 'LONG' | 'SHORT'
  notional: string
  isolatedWallet: string
  updateTime: number
  bidNotional: string
  askNotional: string
}

// ======================== 数据接口类型 ========================

export interface PacificaPriceData {
  symbol: string
  price: string
  time: number
}

export interface PacificaTicker {
  symbol: string
  priceChange: string
  priceChangePercent: string
  weightedAvgPrice: string
  openPrice: string
  highPrice: string
  lowPrice: string
  lastPrice: string
  volume: string
  quoteVolume: string
  openTime: number
  closeTime: number
  firstId: number
  lastId: number
  count: number
}

export interface PacificaOrderBook {
  lastUpdateId: number
  bids: [string, string][] // [price, quantity]
  asks: [string, string][] // [price, quantity]
}

export interface PacificaDepthUpdate {
  e: string // Event type
  E: number // Event time
  s: string // Symbol
  U: number // First update ID in event
  u: number // Final update ID in event
  b: [string, string][] // Bids to be updated
  a: [string, string][] // Asks to be updated
}

export interface PacificaKline {
  symbol: string
  openTime: number
  closeTime: number
  interval: string
  firstTradeId: number
  lastTradeId: number
  open: string
  close: string
  high: string
  low: string
  volume: string
  quoteVolume: string
  trades: number
  takerBuyBaseVolume: string
  takerBuyQuoteVolume: string
}

export interface PacificaTrade {
  id: number
  price: string
  qty: string
  quoteQty: string
  time: number
  isBuyerMaker: boolean
}

export interface PacificaAggTrade {
  a: number // Aggregate tradeId
  p: string // Price
  q: string // Quantity
  f: number // First tradeId
  l: number // Last tradeId
  T: number // Timestamp
  m: boolean // Was the buyer the maker?
  M: boolean // Was the trade the best price match?
}

// ======================== 操作接口类型 ========================

export interface PacificaOrderRequest {
  symbol: string
  side: 'BUY' | 'SELL'
  type: 'LIMIT' | 'MARKET' | 'STOP_LOSS' | 'STOP_LOSS_LIMIT' | 'TAKE_PROFIT' | 'TAKE_PROFIT_LIMIT' | 'LIMIT_MAKER'
  timeInForce?: 'GTC' | 'IOC' | 'FOK'
  quantity?: string
  quoteOrderQty?: string
  price?: string
  newClientOrderId?: string
  strategyId?: number
  strategyType?: number
  stopPrice?: string
  trailingDelta?: number
  icebergQty?: string
  newOrderRespType?: 'ACK' | 'RESULT' | 'FULL'
  selfTradePreventionMode?: 'EXPIRE_TAKER' | 'EXPIRE_MAKER' | 'EXPIRE_BOTH' | 'NONE'
  goodTillDate?: number
}

export interface PacificaOrder {
  symbol: string
  orderId: number
  orderListId: number
  clientOrderId: string
  price: string
  origQty: string
  executedQty: string
  cummulativeQuoteQty: string
  status:
    | 'NEW'
    | 'PARTIALLY_FILLED'
    | 'FILLED'
    | 'CANCELED'
    | 'PENDING_CANCEL'
    | 'REJECTED'
    | 'EXPIRED'
    | 'EXPIRED_IN_MATCH'
  timeInForce: 'GTC' | 'IOC' | 'FOK'
  type: string
  side: 'BUY' | 'SELL'
  stopPrice: string
  icebergQty: string
  time: number
  updateTime: number
  isWorking: boolean
  workingTime: number
  origQuoteOrderQty: string
  selfTradePreventionMode: string
}

export interface PacificaOrderResponse {
  symbol: string
  orderId: number
  orderListId: number
  clientOrderId: string
  transactTime: number
  price: string
  origQty: string
  executedQty: string
  cummulativeQuoteQty: string
  status: string
  timeInForce: string
  type: string
  side: string
  workingTime: number
  selfTradePreventionMode: string
  fills?: PacificaFill[]
}

export interface PacificaFill {
  price: string
  qty: string
  commission: string
  commissionAsset: string
  tradeId: number
}

export interface PacificaCancelOrderRequest {
  symbol: string
  orderId?: number
  origClientOrderId?: string
  newClientOrderId?: string
}

export interface PacificaCancelOrderResponse {
  symbol: string
  orderId: number
  orderListId: number
  clientOrderId: string
  price: string
  origQty: string
  executedQty: string
  cummulativeQuoteQty: string
  status: string
  timeInForce: string
  type: string
  side: string
  selfTradePreventionMode: string
}

export interface PacificaBatchOrderRequest {
  batchOrders: PacificaOrderRequest[]
}

export interface PacificaBatchOrderResponse {
  orders: (PacificaOrderResponse | { code: number; msg: string })[]
}

// ======================== 账户信息类型 ========================

export interface PacificaAccountInfo {
  makerCommission: number
  takerCommission: number
  buyerCommission: number
  sellerCommission: number
  commissionRates: {
    maker: string
    taker: string
    buyer: string
    seller: string
  }
  canTrade: boolean
  canWithdraw: boolean
  canDeposit: boolean
  brokered: boolean
  requireSelfTradePrevention: boolean
  preventSor: boolean
  updateTime: number
  accountType: string
  balances: PacificaBalance[]
  permissions: string[]
  uid: number
}

export interface PacificaFuturesAccountInfo {
  feeTier: number
  canTrade: boolean
  canDeposit: boolean
  canWithdraw: boolean
  updateTime: number
  multiAssetsMargin: boolean
  tradeGroupId: number
  totalInitialMargin: string
  totalMaintMargin: string
  totalWalletBalance: string
  totalUnrealizedProfit: string
  totalMarginBalance: string
  totalPositionInitialMargin: string
  totalOpenOrderInitialMargin: string
  totalCrossWalletBalance: string
  totalCrossUnPnl: string
  availableBalance: string
  maxWithdrawAmount: string
  assets: PacificaFuturesAsset[]
  positions: PacificaPosition[]
}

export interface PacificaFuturesAsset {
  asset: string
  walletBalance: string
  unrealizedProfit: string
  marginBalance: string
  maintMargin: string
  initialMargin: string
  positionInitialMargin: string
  openOrderInitialMargin: string
  maxWithdrawAmount: string
  crossWalletBalance: string
  crossUnPnl: string
  availableBalance: string
  marginAvailable: boolean
  updateTime: number
}

// ======================== WebSocket 类型 ========================

export interface PacificaWebSocketMessage {
  stream: string
  data: any
}

export interface PacificaWebSocketSubscription {
  method: 'SUBSCRIBE' | 'UNSUBSCRIBE'
  params: string[]
  id: number
}

export interface PacificaWebSocketResponse {
  result: any
  id: number
}

// ======================== 错误类型 ========================

export interface PacificaApiError {
  code: number
  msg: string
}

export interface PacificaErrorResponse {
  code: number
  msg: string
  data?: any
}

// ======================== 配置类型 ========================

export interface PacificaConfig {
  baseUrl: string
  wsUrl: string
  apiKey?: string
  secretKey?: string
  privateKey?: string
  testnet?: boolean
  timeout?: number
  recvWindow?: number
  enableProxy?: boolean
  proxyConfig?: {
    host: string
    port: number
    username?: string
    password?: string
  }
}

// ======================== 请求参数类型 ========================

export interface PacificaMarketDataParams {
  symbol?: string
  symbols?: string[]
  limit?: number
  interval?: '1m' | '3m' | '5m' | '15m' | '30m' | '1h' | '2h' | '4h' | '6h' | '8h' | '12h' | '1d' | '3d' | '1w' | '1M'
  startTime?: number
  endTime?: number
  fromId?: number
}

export interface PacificaOrderQueryParams {
  symbol: string
  orderId?: number
  origClientOrderId?: string
  recvWindow?: number
}

export interface PacificaOrderHistoryParams {
  symbol: string
  orderId?: number
  startTime?: number
  endTime?: number
  limit?: number
  recvWindow?: number
}

export interface PacificaTradeHistoryParams {
  symbol: string
  startTime?: number
  endTime?: number
  fromId?: number
  limit?: number
  recvWindow?: number
}

// ======================== 响应包装类型 ========================

export interface PacificaApiResponse<T = any> {
  data: T
  success: boolean
  code?: number
  message?: string
  timestamp: number
}

export interface PacificaListResponse<T = any> {
  data: T[]
  total: number
  page: number
  pageSize: number
  hasNext: boolean
}
