import { apiRequest } from './api.js'

function buildQuery(params = {}) {
  const search = new URLSearchParams()

  if (params.agent) {
    search.set('agent', params.agent)
  }
  if (params.status) {
    search.set('status', params.status)
  }
  if (params.source) {
    search.set('source', params.source)
  }
  if (params.conversationId || params.conversation_id) {
    search.set('conversation_id', params.conversationId || params.conversation_id)
  }
  if (params.keyword) {
    search.set('keyword', params.keyword)
  }
  if (params.limit) {
    search.set('limit', String(params.limit))
  }

  const query = search.toString()
  return query ? `?${query}` : ''
}

export function fetchAgentTraces(params = {}) {
  return apiRequest(`/agent-traces${buildQuery(params)}`)
}

export function fetchAgentTraceDetail(runId) {
  return apiRequest(`/agent-traces/${encodeURIComponent(String(runId || '').trim())}`)
}

export function fetchConversationTrace(conversationId) {
  return apiRequest(`/agent-traces/conversations/${encodeURIComponent(String(conversationId || '').trim())}`)
}
