import { getNearbyTrees, getTreeById } from '@/mock/tree'
import { request } from './request'

const USE_MOCK = false

export async function fetchNearbyTrees(lng: number, lat: number, maxDistance = 10000, limit = 100) {
  if (USE_MOCK) return getNearbyTrees(lng, lat, maxDistance, limit)

  const res = await request<any[]>('/mobile/wishingtree/list', {
    method: 'POST',
    body: JSON.stringify({ lng, lat, maxDistance, limit }),
  })
  return res.data.map((item: any) => ({
    ...item,
    isInRange: item.isInRange ?? false,
  }))
}

export async function fetchTreeDetail(id: number, lng?: number, lat?: number) {
  if (USE_MOCK) return getTreeById(id)

  const res = await request<any>('/mobile/wishingtree/detail', {
    method: 'POST',
    body: JSON.stringify({ id, lng: lng ?? 0, lat: lat ?? 0 }),
  })
  return res.data
}

export async function createTree(data: {
  name: string
  description: string
  longitude: number
  latitude: number
  address: string
  radius: number
  coverImage: string
}) {
  const res = await request<any>('/mobile/admin/create/tree', {
    method: 'POST',
    body: JSON.stringify(data),
  })
  return res.data
}
