/**
 * ERWERB: typed API client for the acquisition (AcquisitionRecord) resource.
 * Captures when/how an animal was acquired + its price. Decoupled from the gerbil
 * (loose nullable ids, no FK) so rows survive the import re-ingest wipe.
 */
import { api } from './client'
import type { DateOnlyString } from './types'

const RESOURCE = '/acquisitions'

export interface Acquisition {
  id: string
  gerbilId: string | null
  sourceContactId: string | null
  date: DateOnlyString | null
  price: number | null
  note: string | null
  createdAt: string
}

/** Payload for POST/PUT /acquisitions. */
export interface AcquisitionInput {
  gerbilId?: string | null
  sourceContactId?: string | null
  date?: DateOnlyString | null
  price?: number | null
  note?: string | null
}

/** All acquisition records for one animal (newest acquisition date first). */
export function listAcquisitions(gerbilId: string): Promise<Acquisition[]> {
  return api.get<Acquisition[]>(`${RESOURCE}?gerbilId=${encodeURIComponent(gerbilId)}`)
}

export function createAcquisition(body: AcquisitionInput): Promise<Acquisition> {
  return api.post<Acquisition>(RESOURCE, body)
}

export function updateAcquisition(id: string, body: AcquisitionInput): Promise<void> {
  return api.put<void>(`${RESOURCE}/${id}`, body)
}

export function deleteAcquisition(id: string): Promise<void> {
  return api.delete(`${RESOURCE}/${id}`)
}
