/**
 * ML Module Types
 * Type definitions for ML predictions, signals, and models
 * Aligned with ml schema DDL (00-enums.sql)
 */

// ============================================================================
// ML Enums (Alineado con ml.* DDL)
// ============================================================================

// Alineado con ml.model_type (DDL)
export type ModelType =
  | 'classification'
  | 'regression'
  | 'time_series'
  | 'clustering'
  | 'anomaly_detection'
  | 'reinforcement_learning';

// Alineado con ml.framework (DDL)
export type MLFramework =
  | 'sklearn'
  | 'tensorflow'
  | 'pytorch'
  | 'xgboost'
  | 'lightgbm'
  | 'prophet'
  | 'custom';

// Alineado con ml.model_status (DDL)
export type ModelStatus =
  | 'development'
  | 'testing'
  | 'staging'
  | 'production'
  | 'deprecated'
  | 'archived';

// Alineado con ml.prediction_type (DDL)
export type PredictionType =
  | 'price_direction'
  | 'price_target'
  | 'volatility'
  | 'trend'
  | 'signal'
  | 'risk_score';

// Alineado con ml.prediction_result (DDL)
export type PredictionResult =
  | 'buy'
  | 'sell'
  | 'hold'
  | 'up'
  | 'down'
  | 'neutral';

// Alineado con ml.outcome_status (DDL)
export type OutcomeStatus =
  | 'pending'
  | 'correct'
  | 'incorrect'
  | 'partially_correct'
  | 'expired';

// ============================================================================
// AMD Phase Types (Custom for Trading Platform)
// ============================================================================

export type AMDPhase =
  | 'accumulation'
  | 'manipulation'
  | 'distribution'
  | 'unknown';

export interface AMDPrediction {
  phase: AMDPhase;
  confidence: number;
  phaseProgress: number; // 0-100%
  expectedDuration?: string;
  signals: string[];
}

// ============================================================================
// Signal Types
// ============================================================================

export type SignalDirection = 'long' | 'short' | 'neutral';
export type SignalStrength = 'weak' | 'moderate' | 'strong' | 'very_strong';
export type SignalTimeframe = '1m' | '5m' | '15m' | '30m' | '1h' | '4h' | '1d';

export interface TradingSignal {
  id: string;
  tenantId: string;
  symbol: string;
  timeframe: SignalTimeframe;
  direction: SignalDirection;
  strength: SignalStrength;
  entryPrice: number;
  stopLoss: number;
  takeProfit: number;
  takeProfitLevels?: number[];
  riskRewardRatio: number;
  confidence: number;
  modelId: string;
  modelName: string;
  amdPhase?: AMDPhase;
  rangePrediction?: RangePrediction;
  tpslPrediction?: TPSLPrediction;
  metadata?: Record<string, unknown>;
  validUntil: string;
  createdAt: string;
}

// ============================================================================
// Range Prediction Types
// ============================================================================

export interface RangePrediction {
  predictedHigh: number;
  predictedLow: number;
  confidence: number;
  volatilityExpected: 'low' | 'medium' | 'high';
  timeHorizon: string;
}

// ============================================================================
// TPSL (Take Profit / Stop Loss) Types
// ============================================================================

export interface TPSLPrediction {
  recommendedTP: number;
  recommendedSL: number;
  tpProbability: number;
  slProbability: number;
  optimalRR: number;
  alternativeLevels?: {
    tp: number[];
    sl: number[];
  };
}

// ============================================================================
// Model Types
// ============================================================================

export interface MLModel {
  id: string;
  tenantId: string;
  name: string;
  displayName: string;
  type: ModelType;
  framework: MLFramework;
  status: ModelStatus;
  version: string;
  accuracy?: number;
  precision?: number;
  recall?: number;
  f1Score?: number;
  lastTrainedAt?: string;
  trainingDataPoints?: number;
  description?: string;
  config?: Record<string, unknown>;
  createdAt: string;
  updatedAt: string;
}

// ============================================================================
// Prediction Package Types (Vendibles)
// ============================================================================

export type PredictionPackageType = 'basic' | 'pro' | 'premium' | 'enterprise';

export interface PredictionPackage {
  id: string;
  tenantId: string;
  name: string;
  type: PredictionPackageType;
  description: string;
  price: number;
  currency: string;
  signalsPerDay: number;
  includesAMD: boolean;
  includesRange: boolean;
  includesTPSL: boolean;
  includesICT: boolean;
  symbols: string[];
  timeframes: SignalTimeframe[];
  features: string[];
  isActive: boolean;
  createdAt: string;
}

export interface PredictionSubscription {
  id: string;
  tenantId: string;
  userId: string;
  packageId: string;
  package?: PredictionPackage;
  status: 'active' | 'cancelled' | 'expired' | 'paused';
  startDate: string;
  endDate: string;
  signalsUsedToday: number;
  signalsRemainingToday: number;
  totalSignalsConsumed: number;
  createdAt: string;
  updatedAt: string;
}

// ============================================================================
// Prediction History Types
// ============================================================================

export interface PredictionRecord {
  id: string;
  tenantId: string;
  userId: string;
  signalId: string;
  signal: TradingSignal;
  outcomeStatus: OutcomeStatus;
  actualEntryPrice?: number;
  actualExitPrice?: number;
  actualResult?: 'profit' | 'loss' | 'breakeven';
  profitLoss?: number;
  profitLossPercentage?: number;
  evaluatedAt?: string;
  createdAt: string;
}

export interface PredictionStats {
  totalPredictions: number;
  correctPredictions: number;
  incorrectPredictions: number;
  pendingPredictions: number;
  accuracy: number;
  profitFactor?: number;
  averageReturn?: number;
  winRate: number;
  bestStreak: number;
  currentStreak: number;
}

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

export interface GetSignalsRequest {
  symbol?: string;
  timeframe?: SignalTimeframe;
  direction?: SignalDirection;
  minConfidence?: number;
  limit?: number;
}

export interface GetSignalsResponse {
  signals: TradingSignal[];
  total: number;
  hasMore: boolean;
}

export interface GetAMDPredictionRequest {
  symbol: string;
  timeframe: SignalTimeframe;
}

export interface GetRangePredictionRequest {
  symbol: string;
  timeframe: SignalTimeframe;
  periods?: number;
}

export interface PredictionPackageResponse {
  packages: PredictionPackage[];
  currentSubscription?: PredictionSubscription;
}
