import client from './client'

export interface Planting {
  id?: number
  garden_id: number
  variety_id: number
  cell_id?: number
  date_plantation?: string
  quantite: number
  statut: string
  notes?: string
}

export interface PlantingEvent {
  id?: number
  planting_id?: number
  type: string
  note?: string
  ts?: string
}

export const plantingsApi = {
  list: () => client.get<Planting[]>('/api/plantings').then(r => r.data),
  get: (id: number) => client.get<Planting>(`/api/plantings/${id}`).then(r => r.data),
  create: (p: Partial<Planting>) => client.post<Planting>('/api/plantings', p).then(r => r.data),
  update: (id: number, p: Partial<Planting>) => client.put<Planting>(`/api/plantings/${id}`, p).then(r => r.data),
  delete: (id: number) => client.delete(`/api/plantings/${id}`),
  events: (id: number) => client.get<PlantingEvent[]>(`/api/plantings/${id}/events`).then(r => r.data),
  addEvent: (id: number, e: Partial<PlantingEvent>) =>
    client.post<PlantingEvent>(`/api/plantings/${id}/events`, e).then(r => r.data),
}
