/**
 * API client for Lowkey Backtest backend.
 */
import axios from 'axios'
import type {
  StrategiesResponse,
  DataStatusResponse,
  BacktestRequest,
  BacktestResult,
  BacktestListResponse,
  CompareResult,
} from './types'

const api = axios.create({
  baseURL: '/api',
  headers: {
    'Content-Type': 'application/json',
  },
})

/**
 * Get list of available strategies with parameters.
 */
export async function getStrategies(): Promise<StrategiesResponse> {
  const response = await api.get<StrategiesResponse>('/strategies')
  return response.data
}

/**
 * Get list of available symbols with data status.
 */
export async function getSymbols(): Promise<DataStatusResponse> {
  const response = await api.get<DataStatusResponse>('/symbols')
  return response.data
}

/**
 * Run a backtest with the given configuration.
 */
export async function runBacktest(request: BacktestRequest): Promise<BacktestResult> {
  const response = await api.post<BacktestResult>('/backtest', request)
  return response.data
}

/**
 * Get list of saved backtest runs.
 */
export async function getBacktests(params?: {
  limit?: number
  offset?: number
  strategy?: string
  symbol?: string
}): Promise<BacktestListResponse> {
  const response = await api.get<BacktestListResponse>('/backtests', { params })
  return response.data
}

/**
 * Get a specific backtest run by ID.
 */
export async function getBacktest(runId: string): Promise<BacktestResult> {
  const response = await api.get<BacktestResult>(`/backtest/${runId}`)
  return response.data
}

/**
 * Delete a backtest run.
 */
export async function deleteBacktest(runId: string): Promise<void> {
  await api.delete(`/backtest/${runId}`)
}

/**
 * Compare multiple backtest runs.
 */
export async function compareRuns(runIds: string[]): Promise<CompareResult> {
  const response = await api.post<CompareResult>('/compare', { run_ids: runIds })
  return response.data
}

export default api
