import axios from 'axios'
import type { Agent, Session, Task, Project, PaginatedResponse } from '../types'

const api = axios.create({
  baseURL: '/api',
  headers: { 'Content-Type': 'application/json' },
})

// Health check
export async function getHealth() {
  const res = await api.get('/health')
  return res.data
}

// Agents
export async function listAgents() {
  const res = await api.get<PaginatedResponse<Agent>>('/agents')
  return res.data
}

export async function getAgent(id: string) {
  const res = await api.get<{ data: Agent }>(`/agents/${id}`)
  return res.data.data
}

export async function createAgent(data: Omit<Agent, 'lastActivity'>) {
  const res = await api.post<{ data: Agent }>('/agents', data)
  return res.data.data
}

export async function updateAgent(id: string, data: Partial<Agent>) {
  const res = await api.put<{ data: Agent }>(`/agents/${id}`, data)
  return res.data.data
}

export async function deleteAgent(id: string) {
  await api.delete(`/agents/${id}`)
}

// Sessions
export async function listSessions() {
  const res = await api.get<PaginatedResponse<Session>>('/sessions')
  return res.data
}

// Tasks
export async function listTasks() {
  const res = await api.get<PaginatedResponse<Task>>('/tasks')
  return res.data
}

// Projects
export async function listProjects() {
  const res = await api.get<PaginatedResponse<Project>>('/projects')
  return res.data
}

export default api
