import { callCloud, canUseCloud } from './cloud'
import { mockNearbyPets } from './mock'
import type { GeoPoint, NearbyPet } from '@/types/domain'

export async function getNearbyPets(location?: GeoPoint, filter = 'all'): Promise<NearbyPet[]> {
  if (!canUseCloud()) return mockNearbyPets
  try {
    const result = await callCloud<object, { list: NearbyPet[] }>('nearbyPets', {
      location,
      filters: { type: filter }
    })
    return result.list
  } catch {
    return mockNearbyPets
  }
}

export async function updateLocation(location: GeoPoint, visible: boolean): Promise<boolean> {
  if (!canUseCloud()) return true
  const result = await callCloud<object, { ok: boolean }>('locationUpdate', { location, visible })
  return result.ok
}

export async function likeNearbyPet(petId: string): Promise<{ matched: boolean }> {
  if (!canUseCloud()) return { matched: petId.length % 2 === 0 }
  return callCloud('matchLike', { petId })
}

