/**
 * Tipos compartidos para Trading Platform Trading Platform
 */

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

export interface ApiResponse<T = unknown> {
  success: boolean;
  data: T;
  message?: string;
  timestamp: string;
  path?: string;
}

export interface PaginatedResponse<T> extends ApiResponse<T[]> {
  pagination: {
    page: number;
    limit: number;
    total: number;
    totalPages: number;
    hasNext: boolean;
    hasPrev: boolean;
  };
}

export interface ApiError {
  success: false;
  error: {
    code: string;
    message: string;
    details?: Record<string, unknown>;
  };
  timestamp: string;
}

// ============================================================================
// Auth Types
// ============================================================================

export interface User {
  id: string;
  email: string;
  name: string;
  avatar?: string;
  role: UserRole;
  status: UserStatus;
  createdAt: string;
  updatedAt: string;
}

export enum UserRole {
  USER = 'user',
  ADMIN = 'admin',
  SUPER_ADMIN = 'super_admin',
}

export enum UserStatus {
  ACTIVE = 'active',
  INACTIVE = 'inactive',
  SUSPENDED = 'suspended',
  PENDING = 'pending',
}

export interface AuthTokens {
  accessToken: string;
  refreshToken: string;
  expiresIn: number;
}

export interface LoginCredentials {
  email: string;
  password: string;
  rememberMe?: boolean;
}

export interface RegisterData {
  email: string;
  password: string;
  name: string;
  acceptTerms: boolean;
}

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

export interface Symbol {
  id: string;
  name: string;
  displayName: string;
  type: SymbolType;
  exchange: string;
  baseCurrency: string;
  quoteCurrency: string;
  pricePrecision: number;
  quantityPrecision: number;
  minQuantity: number;
  maxQuantity: number;
  status: SymbolStatus;
}

export enum SymbolType {
  CRYPTO = 'crypto',
  FOREX = 'forex',
  STOCK = 'stock',
  INDEX = 'index',
  COMMODITY = 'commodity',
}

export enum SymbolStatus {
  ACTIVE = 'active',
  INACTIVE = 'inactive',
  SUSPENDED = 'suspended',
}

export interface Order {
  id: string;
  userId: string;
  symbol: string;
  type: OrderType;
  side: OrderSide;
  quantity: number;
  price?: number;
  stopPrice?: number;
  status: OrderStatus;
  filledQuantity: number;
  averagePrice?: number;
  createdAt: string;
  updatedAt: string;
}

export enum OrderType {
  MARKET = 'market',
  LIMIT = 'limit',
  STOP_LOSS = 'stop_loss',
  STOP_LIMIT = 'stop_limit',
  TAKE_PROFIT = 'take_profit',
}

export enum OrderSide {
  BUY = 'buy',
  SELL = 'sell',
}

export enum OrderStatus {
  PENDING = 'pending',
  OPEN = 'open',
  PARTIALLY_FILLED = 'partially_filled',
  FILLED = 'filled',
  CANCELLED = 'cancelled',
  REJECTED = 'rejected',
  EXPIRED = 'expired',
}

export interface Position {
  id: string;
  userId: string;
  symbol: string;
  side: OrderSide;
  quantity: number;
  entryPrice: number;
  currentPrice: number;
  unrealizedPnl: number;
  realizedPnl: number;
  stopLoss?: number;
  takeProfit?: number;
  leverage: number;
  margin: number;
  createdAt: string;
  updatedAt: string;
}

export interface Trade {
  id: string;
  orderId: string;
  userId: string;
  symbol: string;
  side: OrderSide;
  quantity: number;
  price: number;
  fee: number;
  feeCurrency: string;
  realizedPnl?: number;
  executedAt: string;
}

// ============================================================================
// ML Types
// ============================================================================

export interface Signal {
  id: string;
  symbol: string;
  type: SignalType;
  direction: SignalDirection;
  confidence: number;
  entryPrice: number;
  stopLoss: number;
  takeProfit: number;
  riskReward: number;
  timeframe: string;
  validUntil: string;
  createdAt: string;
  metadata?: Record<string, unknown>;
}

export enum SignalType {
  ENTRY = 'entry',
  EXIT = 'exit',
  ADJUSTMENT = 'adjustment',
}

export enum SignalDirection {
  LONG = 'long',
  SHORT = 'short',
}

export interface Prediction {
  id: string;
  symbol: string;
  timeframe: string;
  predictedHigh: number;
  predictedLow: number;
  predictedDirection: SignalDirection;
  confidence: number;
  modelVersion: string;
  createdAt: string;
  validUntil: string;
}

// ============================================================================
// Agent Types
// ============================================================================

export interface TradingAgent {
  id: string;
  name: string;
  profile: AgentProfile;
  status: AgentStatus;
  balance: number;
  equity: number;
  totalPnl: number;
  winRate: number;
  maxDrawdown: number;
  tradesCount: number;
  createdAt: string;
  updatedAt: string;
}

export enum AgentProfile {
  ATLAS = 'atlas',       // Conservador
  ORION = 'orion',       // Moderado
  NOVA = 'nova',         // Agresivo
}

export enum AgentStatus {
  IDLE = 'idle',
  RUNNING = 'running',
  PAUSED = 'paused',
  STOPPED = 'stopped',
  ERROR = 'error',
}

// ============================================================================
// Portfolio Types
// ============================================================================

export interface Portfolio {
  id: string;
  userId: string;
  name: string;
  type: PortfolioType;
  balance: number;
  equity: number;
  margin: number;
  freeMargin: number;
  marginLevel: number;
  unrealizedPnl: number;
  realizedPnl: number;
  currency: string;
  createdAt: string;
  updatedAt: string;
}

export enum PortfolioType {
  REAL = 'real',
  DEMO = 'demo',
  PAPER = 'paper',
}

// ============================================================================
// Education Types
// ============================================================================

export interface Course {
  id: string;
  title: string;
  description: string;
  category: string;
  difficulty: CourseDifficulty;
  duration: number;
  lessonsCount: number;
  enrolledCount: number;
  rating: number;
  thumbnail?: string;
  instructor: string;
  status: CourseStatus;
  createdAt: string;
  updatedAt: string;
}

export enum CourseDifficulty {
  BEGINNER = 'beginner',
  INTERMEDIATE = 'intermediate',
  ADVANCED = 'advanced',
  EXPERT = 'expert',
}

export enum CourseStatus {
  DRAFT = 'draft',
  PUBLISHED = 'published',
  ARCHIVED = 'archived',
}

// ============================================================================
// Subscription Types
// ============================================================================

export interface Subscription {
  id: string;
  userId: string;
  plan: SubscriptionPlan;
  status: SubscriptionStatus;
  currentPeriodStart: string;
  currentPeriodEnd: string;
  cancelAtPeriodEnd: boolean;
  stripeSubscriptionId?: string;
  createdAt: string;
  updatedAt: string;
}

export enum SubscriptionPlan {
  FREE = 'free',
  BASIC = 'basic',
  PRO = 'pro',
  ENTERPRISE = 'enterprise',
}

export enum SubscriptionStatus {
  ACTIVE = 'active',
  INACTIVE = 'inactive',
  CANCELLED = 'cancelled',
  PAST_DUE = 'past_due',
  TRIALING = 'trialing',
}

// ============================================================================
// WebSocket Types
// ============================================================================

export interface WSMessage<T = unknown> {
  type: WSMessageType;
  channel?: string;
  data: T;
  timestamp: string;
}

export enum WSMessageType {
  // Connection
  CONNECT = 'connect',
  DISCONNECT = 'disconnect',
  PING = 'ping',
  PONG = 'pong',

  // Subscriptions
  SUBSCRIBE = 'subscribe',
  UNSUBSCRIBE = 'unsubscribe',

  // Market Data
  TICKER = 'ticker',
  ORDERBOOK = 'orderbook',
  TRADE = 'trade',
  CANDLE = 'candle',

  // User Data
  ORDER_UPDATE = 'order_update',
  POSITION_UPDATE = 'position_update',
  BALANCE_UPDATE = 'balance_update',

  // Signals
  SIGNAL = 'signal',
  PREDICTION = 'prediction',

  // Notifications
  NOTIFICATION = 'notification',
}

export interface Ticker {
  symbol: string;
  price: number;
  change24h: number;
  changePercent24h: number;
  high24h: number;
  low24h: number;
  volume24h: number;
  timestamp: string;
}

export interface Candle {
  symbol: string;
  timeframe: string;
  open: number;
  high: number;
  low: number;
  close: number;
  volume: number;
  timestamp: string;
}
