import { callCloud, canUseCloud } from './cloud'
import { mockPets, mockProfile } from './mock'
import { normalizeUser } from '@/utils/user'
import type { Pet, UserProfile } from '@/types/domain'

export async function getProfile(): Promise<{ user: UserProfile; pets: Pet[] }> {
  if (!canUseCloud()) return { user: mockProfile, pets: mockPets }
  try {
    const result = await callCloud<object, { user: UserProfile; pets: Pet[] }>('profileGet', {})
    return { user: normalizeUser(result.user), pets: result.pets || [] }
  } catch {
    return { user: mockProfile, pets: mockPets }
  }
}

export async function updateProfile(patch: Partial<UserProfile>): Promise<UserProfile> {
  if (!canUseCloud()) return { ...mockProfile, ...patch }
  const result = await callCloud<object, { user: UserProfile }>('profileUpdate', { patch })
  return normalizeUser(result.user)
}

export async function getPetList(): Promise<Pet[]> {
  if (!canUseCloud()) return mockPets
  const result = await callCloud<object, { list: Pet[] }>('petList', {})
  return result.list
}

