/**
 * Pest Detection API
 * @see RECOMMENDATION_APIS.md
 */

import { apiClient } from '../client'

const PREFIX = '/api/pest-detection'

export interface PestAnalyzeResult {
  pest: string
  confidence: number
  description: string
  treatment: string
}

interface ApiResponse<T> {
  status: string
  data: T
}

async function unwrap<T>(promise: Promise<ApiResponse<T>>): Promise<T> {
  const res = await promise
  return res.data
}

export const pestDetectionService = {
  /**
   * Analyze image (optional FormData with 'image' or 'file' key). Returns mock result if backend does not use image yet.
   */
  analyze(formData?: FormData): Promise<PestAnalyzeResult> {
    if (formData) {
      return unwrap(apiClient.postFormData<ApiResponse<PestAnalyzeResult>>(`${PREFIX}/analyze/`, formData))
    }
    return unwrap(apiClient.post<ApiResponse<PestAnalyzeResult>>(`${PREFIX}/analyze/`, {}))
  },
}
