import { apiClient } from './client'
import { PortfolioResponse, PortfolioHistoryResponse } from '../types'

export const portfolioApi = {
  getCurrentPortfolio: async (paperTrading: boolean = true): Promise<PortfolioResponse> => {
    const response = await apiClient.get('/api/portfolio/current', {
      params: { paper_trading: paperTrading },
    })
    return response.data
  },

  getPortfolioHistory: async (days: number = 30, paperTrading: boolean = true): Promise<PortfolioHistoryResponse> => {
    const response = await apiClient.get('/api/portfolio/history', {
      params: { days, paper_trading: paperTrading },
    })
    return response.data
  },

  updatePositionsPrices: async (prices: Record<string, number>, paperTrading: boolean = true): Promise<void> => {
    await apiClient.post('/api/portfolio/positions/update-prices', prices, {
      params: { paper_trading: paperTrading },
    })
  },

  getRiskMetrics: async (days: number = 30, paperTrading: boolean = true): Promise<{
    total_return: number
    total_return_percent: number
    sharpe_ratio: number
    sortino_ratio: number
    max_drawdown: number
    current_drawdown: number
    win_rate: number
    initial_value: number
    final_value: number
  }> => {
    const response = await apiClient.get('/api/portfolio/risk-metrics', {
      params: { days, paper_trading: paperTrading },
    })
    return response.data
  },
}
