import axios from 'axios'
import type { FilamentSpool, ListResponse, FilamentFilter } from '../types/filament'

const API_BASE = '/api'

export async function fetchFilaments(filter: FilamentFilter): Promise<ListResponse<FilamentSpool>> {
  const params = new URLSearchParams()
  if (filter.material) params.set('material', filter.material)
  if (filter.finish) params.set('finish', filter.finish)
  if (filter.color) params.set('color', filter.color)
  if (filter.low_stock) params.set('low_stock', 'true')
  if (filter.search) params.set('search', filter.search)
  if (filter.sort_by) params.set('sort_by', filter.sort_by)
  if (filter.sort_dir) params.set('sort_dir', filter.sort_dir)
  if (filter.limit !== undefined) params.set('limit', String(filter.limit))
  if (filter.offset !== undefined) params.set('offset', String(filter.offset))

  const res = await axios.get<ListResponse<FilamentSpool>>(`${API_BASE}/filaments?${params.toString()}`)
  return res.data
}

export async function deleteFilament(id: number): Promise<void> {
  await axios.delete(`${API_BASE}/filaments/${id}`)
}
