import { createStorageProvider, type StorageProviderType } from '@/lib/storage'

/**
 * Generate a pre-signed download URL for a user's avatar.
 * Returns null if the user has no avatar.
 */
export async function getUserAvatarUrl(
  profileImageKey: string | null | undefined,
  profileImageProvider: string | null | undefined
): Promise<string | null> {
  if (!profileImageKey) return null

  try {
    const providerType = (profileImageProvider as StorageProviderType) || 's3'
    const provider = createStorageProvider(providerType)
    return await provider.getDownloadUrl(profileImageKey)
  } catch (error) {
    console.error('[AvatarURL] Failed to generate URL for key:', profileImageKey, error)
    return null
  }
}

/**
 * Batch-generate avatar URLs for multiple users.
 * Adds `avatarUrl` field to each user object.
 */
export async function attachAvatarUrls<
  T extends { profileImageKey?: string | null; profileImageProvider?: string | null }
>(users: T[]): Promise<(T & { avatarUrl: string | null })[]> {
  return Promise.all(
    users.map(async (user) => ({
      ...user,
      avatarUrl: await getUserAvatarUrl(user.profileImageKey, user.profileImageProvider),
    }))
  )
}
