/**
 * ML Engine Service
 * Client for connecting to the ML Engine API
 */

const ML_API_URL = import.meta.env.VITE_ML_URL || 'http://localhost:8001';

// ============================================================================
// Types
// ============================================================================

export interface MLSignal {
  signal_id: string;
  symbol: string;
  direction: 'long' | 'short';
  entry_price: number;
  stop_loss: number;
  take_profit: number;
  risk_reward_ratio: number;
  confidence_score: number;
  prob_tp_first: number;
  amd_phase: string;
  volatility_regime: string;
  valid_until: string;
  created_at: string;
}

export interface AMDPhase {
  symbol: string;
  phase: 'accumulation' | 'manipulation' | 'distribution' | 'unknown';
  confidence: number;
  phase_duration_bars: number;
  next_phase_probability: {
    accumulation: number;
    manipulation: number;
    distribution: number;
  };
  key_levels: {
    support: number;
    resistance: number;
  };
}

export interface RangePrediction {
  symbol: string;
  timeframe: string;
  current_price: number;
  predicted_high: number;
  predicted_low: number;
  expected_range_percent: number;
  volatility_regime: 'low' | 'normal' | 'high' | 'extreme';
  prediction_confidence: number;
}

export interface BacktestResult {
  strategy: string;
  symbol: string;
  start_date: string;
  end_date: string;
  total_trades: number;
  winning_trades: number;
  losing_trades: number;
  win_rate: number;
  total_pnl: number;
  max_drawdown: number;
  sharpe_ratio: number;
  profit_factor: number;
}

// ============================================================================
// API Functions
// ============================================================================

/**
 * Get the latest ML signal for a symbol
 */
export async function getLatestSignal(symbol: string): Promise<MLSignal | null> {
  try {
    const response = await fetch(`${ML_API_URL}/api/v1/signals/latest/${symbol}`);
    if (!response.ok) {
      if (response.status === 404) return null;
      throw new Error(`API error: ${response.status}`);
    }
    const data = await response.json();
    return data.signal || null;
  } catch (error) {
    console.error('Error fetching latest signal:', error);
    return null;
  }
}

/**
 * Get active signals for all symbols
 */
export async function getActiveSignals(): Promise<MLSignal[]> {
  try {
    const response = await fetch(`${ML_API_URL}/api/v1/signals/active`);
    if (!response.ok) {
      throw new Error(`API error: ${response.status}`);
    }
    const data = await response.json();
    return data.signals || [];
  } catch (error) {
    console.error('Error fetching active signals:', error);
    return [];
  }
}

/**
 * Get AMD phase detection for a symbol
 */
export async function getAMDPhase(symbol: string): Promise<AMDPhase | null> {
  try {
    const response = await fetch(`${ML_API_URL}/api/v1/amd/detect/${symbol}`);
    if (!response.ok) {
      if (response.status === 404) return null;
      throw new Error(`API error: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Error fetching AMD phase:', error);
    return null;
  }
}

/**
 * Get range prediction for a symbol
 */
export async function getRangePrediction(
  symbol: string,
  timeframe: string = '1h'
): Promise<RangePrediction | null> {
  try {
    const response = await fetch(
      `${ML_API_URL}/api/v1/predict/range/${symbol}?timeframe=${timeframe}`
    );
    if (!response.ok) {
      if (response.status === 404) return null;
      throw new Error(`API error: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Error fetching range prediction:', error);
    return null;
  }
}

/**
 * Generate a new signal for a symbol
 */
export async function generateSignal(symbol: string): Promise<MLSignal | null> {
  try {
    const response = await fetch(`${ML_API_URL}/api/v1/signals/generate`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ symbol }),
    });
    if (!response.ok) {
      throw new Error(`API error: ${response.status}`);
    }
    const data = await response.json();
    return data.signal || null;
  } catch (error) {
    console.error('Error generating signal:', error);
    return null;
  }
}

/**
 * Run backtest for a strategy
 */
export async function runBacktest(params: {
  strategy: string;
  symbol: string;
  start_date: string;
  end_date: string;
  initial_capital?: number;
}): Promise<BacktestResult | null> {
  try {
    const response = await fetch(`${ML_API_URL}/api/v1/backtest/run`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify(params),
    });
    if (!response.ok) {
      throw new Error(`API error: ${response.status}`);
    }
    const data = await response.json();
    return data;
  } catch (error) {
    console.error('Error running backtest:', error);
    return null;
  }
}

/**
 * Check ML Engine health
 */
export async function checkHealth(): Promise<boolean> {
  try {
    const response = await fetch(`${ML_API_URL}/health`);
    return response.ok;
  } catch {
    return false;
  }
}
