import { callCloud, canUseCloud } from './cloud'
import type { FollowCounts, SocialUser } from '@/types/domain'

export type FollowListType = 'friends' | 'following' | 'followers'

const emptyCounts: FollowCounts = { following: 0, followers: 0, friends: 0 }

export async function getFollowList(type: FollowListType): Promise<{ list: SocialUser[]; counts: FollowCounts }> {
  if (!canUseCloud()) return { list: [], counts: emptyCounts }
  try {
    return await callCloud('followList', { type })
  } catch {
    return { list: [], counts: emptyCounts }
  }
}

export async function discoverUsers(keyword?: string): Promise<SocialUser[]> {
  if (!canUseCloud()) return []
  try {
    const res = await callCloud<object, { list: SocialUser[] }>('userList', { keyword: keyword || '' })
    return res.list
  } catch {
    return []
  }
}

export async function toggleFollow(targetUserId: string, follow: boolean): Promise<{ following: boolean; mutual: boolean }> {
  if (!canUseCloud()) return { following: follow, mutual: false }
  return callCloud('followToggle', { targetUserId, follow })
}
