import { EventEmitter } from 'events'

export type Side = 'BUY' | 'SELL'
export type OrderType = 'MARKET' | 'LIMIT' | 'POST_ONLY'
export type TimeInForce = 'GTC' | 'IOC' | 'FOK'
export type PositionSide = 'LONG' | 'SHORT'

export interface PlaceOrderReq {
  symbol: string
  side: Side
  type: OrderType
  quantity: string
  price?: string
  tif?: TimeInForce
  reduceOnly?: boolean
  clientOrderId?: string
}

export interface Order {
  id: string
  symbol: string
  status: 'NEW' | 'PARTIALLY_FILLED' | 'FILLED' | 'CANCELED' | 'REJECTED' | 'EXPIRED'
  side: Side
  type: OrderType
  price?: string
  origQty: string
  executedQty: string
  updateTime: number
}

export interface Position {
  symbol: string
  side: PositionSide
  qty: string
  entryPrice: string
  unrealizedPnl: string
  leverage?: number
}

export interface Balance {
  asset: string
  total: string
  free: string
}

export interface DepthLevel {
  price: string
  qty: string
}
export interface Depth {
  bids: DepthLevel[]
  asks: DepthLevel[]
  ts: number
}

export interface ExchangeAdapter {
  name(): string
  time(): Promise<number>
  symbols(): Promise<string[]>
  balances(): Promise<Balance[]>
  positions(): Promise<Position[]>
  placeOrder(req: PlaceOrderReq): Promise<Order>
  cancelOrder(symbol: string, orderId: string): Promise<void>
  cancelAll(symbol: string): Promise<void>
  getOrder(symbol: string, orderId: string): Promise<Order>
  openOrders(symbol: string): Promise<Order[]>
  leverage?(symbol: string, lev: number): Promise<void>
  depth(symbol: string, limit?: number): Promise<Depth>
  ws(): EventEmitter
}


