import api from './index'
import type { AxiosResponse } from 'axios'

export interface Log {
  id: string
  level: 'debug' | 'info' | 'warning' | 'error'
  type: 'agent' | 'system' | 'chat'
  user_id: string | null
  request_id: string | null
  route: string | null
  method: string | null
  status_code: number | null
  error_type: string | null
  operation: string | null
  message: string
  source: string | null
  details: Record<string, unknown> | null
  duration_ms: number | null
  created_at: string | null
  updated_at: string | null
}

export interface LogStats {
  total: number
  by_type: {
    agent: number
    system: number
    chat: number
  }
  by_level: {
    debug: number
    info: number
    warning: number
    error: number
  }
}

export interface LogQueryResult {
  logs: Log[]
  total: number
  page: number
  page_size: number
}

export interface LogQueryParams {
  log_type?: string
  level?: string
  source?: string
  request_id?: string
  route?: string
  operation?: string
  status_code?: number
  start_at?: string
  end_at?: string
  page?: number
  page_size?: number
}

export const logApi = {
  list: (params?: LogQueryParams): Promise<AxiosResponse<LogQueryResult>> => {
    return api.get('/api/logs', { params })
  },

  getStats: (params?: LogQueryParams): Promise<AxiosResponse<LogStats>> => {
    return api.get('/api/logs/stats', { params })
  },

  getRecent: (params?: {
    log_type?: string
    hours?: number
    limit?: number
  }): Promise<AxiosResponse<Log[]>> => {
    return api.get('/api/logs/recent', { params })
  },
}
