import request from '@/utils/http'

export interface APIKeyRecord {
  id: string
  name: string
  key_prefix: string
  scopes: string[]
  enabled: boolean
  requests_per_minute: number
  monthly_request_quota: number
  monthly_token_quota: number
  monthly_token_usage: number
  expires_at: string | null
  last_used_at: string | null
  created_at: string
  key?: string
  warning?: string
}

export function fetchAPIKeys() {
  return request.get<APIKeyRecord[]>({ url: '/api/v1/admin/api-keys' })
}

export function createAPIKey(data: {
  name: string
  scopes: string[]
  requests_per_minute: number
  monthly_request_quota: number
  monthly_token_quota: number
  expires_at: string | null
}) {
  return request.post<APIKeyRecord>({ url: '/api/v1/admin/api-keys', params: data })
}

export function revokeAPIKey(id: string) {
  return request.del<{ revoked: boolean }>({ url: `/api/v1/admin/api-keys/${id}` })
}

export function updateAPIKeyLimits(id: string, data: {
  requests_per_minute: number
  monthly_request_quota: number
  monthly_token_quota: number
}) {
  return request.put<APIKeyRecord>({ url: `/api/v1/admin/api-keys/${id}/limits`, params: data })
}
