import type { UsageStats, StatsQueryParams } from '@/types'
import { request } from './client'

export async function getStats(params?: StatsQueryParams): Promise<UsageStats[]> {
  if (!params) {
    return request<UsageStats[]>('GET', '/api/stats')
  }

  const query = new URLSearchParams()
  const snakeParams: Record<string, string | undefined> = {
    provider_id: params.providerId,
    model_name: params.modelName,
    start_date: params.startDate,
    end_date: params.endDate,
  }

  for (const [key, value] of Object.entries(snakeParams)) {
    if (value) {
      query.set(key, value)
    }
  }

  const queryString = query.toString()
  const path = queryString ? `/api/stats?${queryString}` : '/api/stats'
  return request<UsageStats[]>('GET', path)
}
