package interfaces

import (
	"go-admin/models"

	"github.com/shopspring/decimal"
)

// ExchangeInterface 交易所通用接口抽象层
type ExchangeInterface interface {
	// 基础信息接口
	GetExchangeName() string
	GetExchangeInfo() (ExchangeInfoResponse, error)

	// 现货市场接口
	SpotInterface

	// 合约接口（如果支持）
	FuturesInterface

	// WebSocket接口
	WebSocketInterface
}

// SpotInterface 现货交易接口
type SpotInterface interface {
	// 市场数据
	GetSpotTicker24h(symbol string) (models.Ticker24, error)
	GetSpotTickers() ([]models.TradeSet, error)
	// 订单管理
	PlaceSpotOrder(params SpotOrderParams) (SpotOrderResponse, error)
	CancelSpotOrder(symbol, orderID string) error
	GetSpotOrder(symbol, orderID string) (SpotOrderResponse, error)
	GetSpotOrders(symbol string) ([]SpotOrderResponse, error)
}

// FuturesInterface 合约交易接口
type FuturesInterface interface {
	// 市场数据
	GetFuturesTicker24h(symbol string) (models.Ticker24, error)
	GetFuturesTickers() ([]models.TradeSet, error)
	// 订单管理
	PlaceFuturesOrder(params FuturesOrderParams) (FuturesOrderResponse, error)
	CancelFuturesOrder(symbol, orderID string) error
	GetFuturesOrder(symbol, orderID string) (FuturesOrderResponse, error)
}

// WebSocketInterface WebSocket接口
type WebSocketInterface interface {
	SubscribeSpotTicker(symbols []string, callback func(models.Ticker24, string, string)) error
	SubscribeFuturesTicker(symbols []string, callback func(models.Ticker24, string, string)) error
	SubscribeSpotDepth(symbols []string, callback func(models.DepthBin, string, string)) error
	SubscribeSpotTrades(symbols []string, callback func(models.NewDealPush, string, string)) error
	SubscribeKline(symbols []string, interval string, callback func(models.Kline, int, string, string)) error

	Close() error
}

// 通用数据结构定义
type ExchangeInfoResponse struct {
	Symbols []SymbolInfo `json:"symbols"`
}

type SymbolInfo struct {
	Symbol      string `json:"symbol"`
	BaseAsset   string `json:"baseAsset"`
	QuoteAsset  string `json:"quoteAsset"`
	Status      string `json:"status"`
	MinQty      string `json:"minQty"`
	MaxQty      string `json:"maxQty"`
	StepSize    string `json:"stepSize"`
	MinPrice    string `json:"minPrice"`
	MaxPrice    string `json:"maxPrice"`
	TickSize    string `json:"tickSize"`
	MinNotional string `json:"minNotional"`
}

// 现货订单参数
type SpotOrderParams struct {
	Symbol           string          `json:"symbol"`
	Side             string          `json:"side"` // BUY/SELL
	Type             string          `json:"type"` // MARKET/LIMIT
	Quantity         decimal.Decimal `json:"quantity"`
	Price            decimal.Decimal `json:"price,omitempty"`
	TimeInForce      string          `json:"timeInForce,omitempty"` // GTC/IOC/FOK
	NewClientOrderId string          `json:"newClientOrderId,omitempty"`
}

// 合约订单参数
type FuturesOrderParams struct {
	Symbol           string          `json:"symbol"`
	Side             string          `json:"side"` // BUY/SELL
	Type             string          `json:"type"` // MARKET/LIMIT
	Quantity         decimal.Decimal `json:"quantity"`
	Price            decimal.Decimal `json:"price,omitempty"`
	TimeInForce      string          `json:"timeInForce,omitempty"`
	PositionSide     string          `json:"positionSide,omitempty"` // LONG/SHORT/BOTH
	NewClientOrderId string          `json:"newClientOrderId,omitempty"`
}

// 响应数据结构
type SpotAccountResponse struct {
	Balances []SpotBalanceResponse `json:"balances"`
}

type SpotBalanceResponse struct {
	Asset  string `json:"asset"`
	Free   string `json:"free"`
	Locked string `json:"locked"`
}

type SpotOrderResponse struct {
	Symbol        string `json:"symbol"`
	OrderId       int64  `json:"orderId"`
	ClientOrderId string `json:"clientOrderId"`
	Price         string `json:"price"`
	OrigQty       string `json:"origQty"`
	ExecutedQty   string `json:"executedQty"`
	Status        string `json:"status"`
	TimeInForce   string `json:"timeInForce"`
	Type          string `json:"type"`
	Side          string `json:"side"`
	Time          int64  `json:"time"`
	UpdateTime    int64  `json:"updateTime"`
}

type FuturesAccountResponse struct {
	TotalWalletBalance string                    `json:"totalWalletBalance"`
	Assets             []FuturesBalanceResponse  `json:"assets"`
	Positions          []FuturesPositionResponse `json:"positions"`
}

type FuturesBalanceResponse struct {
	Asset                  string `json:"asset"`
	WalletBalance          string `json:"walletBalance"`
	UnrealizedProfit       string `json:"unrealizedProfit"`
	MarginBalance          string `json:"marginBalance"`
	MaintMargin            string `json:"maintMargin"`
	InitialMargin          string `json:"initialMargin"`
	PositionInitialMargin  string `json:"positionInitialMargin"`
	OpenOrderInitialMargin string `json:"openOrderInitialMargin"`
}

type FuturesPositionResponse struct {
	Symbol           string `json:"symbol"`
	PositionAmt      string `json:"positionAmt"`
	EntryPrice       string `json:"entryPrice"`
	MarkPrice        string `json:"markPrice"`
	UnrealizedProfit string `json:"unrealizedProfit"`
	LiquidationPrice string `json:"liquidationPrice"`
	Leverage         string `json:"leverage"`
	PositionSide     string `json:"positionSide"`
}

type FuturesOrderResponse struct {
	Symbol        string `json:"symbol"`
	OrderId       int64  `json:"orderId"`
	ClientOrderId string `json:"clientOrderId"`
	Price         string `json:"price"`
	OrigQty       string `json:"origQty"`
	ExecutedQty   string `json:"executedQty"`
	Status        string `json:"status"`
	TimeInForce   string `json:"timeInForce"`
	Type          string `json:"type"`
	Side          string `json:"side"`
	PositionSide  string `json:"positionSide"`
	ReduceOnly    bool   `json:"reduceOnly"`
	Time          int64  `json:"time"`
	UpdateTime    int64  `json:"updateTime"`
}
