import client from './client'

export interface Garden {
  id?: number
  nom: string
  description?: string
  type: string
  latitude?: number
  longitude?: number
  adresse?: string
  exposition?: string
  ombre?: string
  sol_type?: string
  grille_largeur: number
  grille_hauteur: number
}

export interface GardenCell {
  id?: number
  garden_id?: number
  col: number
  row: number
  libelle?: string
  etat: string
  notes?: string
}

export interface Measurement {
  id?: number
  garden_id?: number
  temp_air?: number
  temp_sol?: number
  humidite_air?: number
  humidite_sol?: number
  source?: string
  ts?: string
}

export const gardensApi = {
  list: () => client.get<Garden[]>('/api/gardens').then(r => r.data),
  get: (id: number) => client.get<Garden>(`/api/gardens/${id}`).then(r => r.data),
  create: (g: Partial<Garden>) => client.post<Garden>('/api/gardens', g).then(r => r.data),
  update: (id: number, g: Partial<Garden>) => client.put<Garden>(`/api/gardens/${id}`, g).then(r => r.data),
  delete: (id: number) => client.delete(`/api/gardens/${id}`),
  cells: (id: number) => client.get<GardenCell[]>(`/api/gardens/${id}/cells`).then(r => r.data),
  measurements: (id: number) => client.get<Measurement[]>(`/api/gardens/${id}/measurements`).then(r => r.data),
  addMeasurement: (id: number, m: Partial<Measurement>) =>
    client.post<Measurement>(`/api/gardens/${id}/measurements`, m).then(r => r.data),
}
