/**
 * Trading Types
 * Type definitions for trading features
 */

import { Time } from 'lightweight-charts';

// ============================================================================
// Market Data Types
// ============================================================================

export type Interval = '1m' | '5m' | '15m' | '30m' | '1h' | '4h' | '1d' | '1w';

export interface Candle {
  time: number; // Unix timestamp in milliseconds
  open: number;
  high: number;
  low: number;
  close: number;
  volume: number;
}

export interface Ticker {
  symbol: string;
  price: number;
  change: number;
  changePercent: number;
  high24h: number;
  low24h: number;
  volume24h: number;
  quoteVolume24h: number;
}

export interface OrderBookEntry {
  price: number;
  quantity: number;
  total: number;
}

export interface OrderBook {
  symbol: string;
  bids: OrderBookEntry[];
  asks: OrderBookEntry[];
  timestamp: number;
}

export interface TradingSymbol {
  symbol: string;
  baseAsset: string;
  quoteAsset: string;
  status: string;
}

// ============================================================================
// Indicator Types
// ============================================================================

export interface SMAData {
  time: number;
  value: number;
}

export interface EMAData {
  time: number;
  value: number;
}

export interface RSIData {
  time: number;
  value: number;
}

export interface MACDData {
  time: number;
  macd: number;
  signal: number;
  histogram: number;
}

export interface BollingerBandsData {
  time: number;
  upper: number;
  middle: number;
  lower: number;
}

// ============================================================================
// Chart Types
// ============================================================================

export interface CandleData {
  time: Time;
  open: number;
  high: number;
  low: number;
  close: number;
}

export interface VolumeData {
  time: Time;
  value: number;
  color: string;
}

export interface CrosshairData {
  time: Time;
  price: number;
}

// ============================================================================
// API Response Types
// ============================================================================

export interface ApiResponse<T> {
  success: boolean;
  data: T;
  message?: string;
  error?: string;
}

export interface KlinesResponse {
  symbol: string;
  interval: Interval;
  klines: Candle[];
}

export interface TickerResponse {
  symbol: string;
  ticker: Ticker;
}

export interface TickersResponse {
  tickers: Ticker[];
}

export interface OrderBookResponse {
  symbol: string;
  orderbook: OrderBook;
}

export interface SymbolSearchResponse {
  symbols: TradingSymbol[];
}

// ============================================================================
// Store Types
// ============================================================================

export interface TradingState {
  // Selected market
  selectedSymbol: string;
  timeframe: Interval;

  // Market data
  klines: Candle[];
  currentTicker: Ticker | null;
  orderBook: OrderBook | null;

  // Loading states
  loadingKlines: boolean;
  loadingTicker: boolean;
  loadingOrderBook: boolean;

  // Errors
  error: string | null;

  // Actions
  setSymbol: (symbol: string) => void;
  setTimeframe: (timeframe: Interval) => void;
  fetchKlines: (symbol?: string, interval?: Interval, limit?: number) => Promise<void>;
  fetchTicker: (symbol?: string) => Promise<void>;
  fetchOrderBook: (symbol?: string, limit?: number) => Promise<void>;
  clearError: () => void;
}

// ============================================================================
// Watchlist Types
// ============================================================================

export interface Watchlist {
  id: string;
  userId: string;
  name: string;
  isDefault: boolean;
  createdAt: Date;
  updatedAt: Date;
  items: WatchlistItemData[];
}

export interface WatchlistItemData {
  id: string;
  watchlistId: string;
  symbol: string;
  sortOrder: number;
  notes?: string;
  createdAt: Date;
}

export interface WatchlistSymbolData {
  symbol: string;
  lastPrice: number;
  priceChange: number;
  priceChangePercent: number;
  high24h: number;
  low24h: number;
  volume24h: number;
}

// ============================================================================
// Paper Trading Types
// ============================================================================

export interface PaperAccount {
  id: string;
  userId: string;
  name: string;
  initialBalance: number;
  currentBalance: number;
  currency: string;
  totalTrades: number;
  winningTrades: number;
  totalPnl: number;
  maxDrawdown: number;
  isActive: boolean;
}

export interface PaperPosition {
  id: string;
  accountId: string;
  symbol: string;
  direction: 'long' | 'short';
  lotSize: number;
  entryPrice: number;
  stopLoss?: number;
  takeProfit?: number;
  exitPrice?: number;
  status: 'open' | 'closed' | 'pending';
  openedAt: Date;
  closedAt?: Date;
  closeReason?: string;
  realizedPnl?: number;
  // Calculated fields
  currentPrice?: number;
  unrealizedPnl?: number;
  unrealizedPnlPercent?: number;
}

export interface PaperOrder {
  id: string;
  accountId: string;
  symbol: string;
  direction: 'long' | 'short';
  lotSize: number;
  orderType: 'market' | 'limit';
  entryPrice?: number;
  stopLoss?: number;
  takeProfit?: number;
  status: 'pending' | 'filled' | 'cancelled';
  createdAt: Date;
  filledAt?: Date;
}

export interface CreateOrderInput {
  symbol: string;
  direction: 'long' | 'short';
  lotSize: number;
  orderType: 'market' | 'limit';
  entryPrice?: number;
  stopLoss?: number;
  takeProfit?: number;
}

export interface PaperTrade {
  id: string;
  positionId: string;
  symbol: string;
  direction: 'long' | 'short';
  lotSize: number;
  entryPrice: number;
  exitPrice: number;
  realizedPnl: number;
  realizedPnlPercent: number;
  openedAt: Date;
  closedAt: Date;
  closeReason: string;
}

export interface AccountSummary {
  account: PaperAccount;
  openPositions: number;
  totalEquity: number;
  unrealizedPnl: number;
  todayPnl: number;
  winRate: number;
}

export interface PaperBalance {
  balance: number;
  equity: number;
  unrealizedPnl: number;
  currency: string;
}

// ============================================================================
// Component Props Types
// ============================================================================

export interface CandlestickChartProps {
  symbol: string;
  interval?: Interval;
  height?: number;
  theme?: 'dark' | 'light';
  showVolume?: boolean;
  onCrosshairMove?: (data: CrosshairData | null) => void;
}

export interface ChartToolbarProps {
  symbol: string;
  interval: Interval;
  symbols?: string[];
  onSymbolChange: (symbol: string) => void;
  onIntervalChange: (interval: Interval) => void;
  onIndicatorToggle?: (indicator: string) => void;
  theme?: 'dark' | 'light';
}
