import request from '@/utils/http'

export interface TraceSpan {
  id: string
  trace_id: string
  parent_id?: string
  span_type: 'model' | 'tool' | 'retrieval'
  name: string
  status: 'running' | 'success' | 'error'
  started_at: string
  finished_at?: string
  latency_ms?: number
  provider_code?: string
  model?: string
  input_tokens: number
  output_tokens: number
  round: number
  error: string
  metadata: Record<string, unknown>
}

export interface Trace {
  id: string
  request_id: string
  api_key_id?: string
  tenant_id?: string
  trace_type: 'application' | 'digital_employee'
  target_id?: string
  target_code: string
  conversation_id: string
  status: 'running' | 'success' | 'error'
  started_at: string
  finished_at?: string
  latency_ms?: number
  retrieval_count: number
  model_call_count: number
  tool_call_count: number
  error: string
  metadata: Record<string, unknown>
  spans?: TraceSpan[]
}

export interface TracePage { items: Trace[] }

export function fetchTraces(params: Record<string, string | number | undefined>) {
  return request.get<TracePage>({ url: '/api/v1/admin/traces', params })
}

export function fetchTrace(id: string) {
  return request.get<Trace>({ url: `/api/v1/admin/traces/${id}` })
}
