export default defineEventHandler(async (event) => {
  const userId: string = event.context.params ? event.context.params.id : '@me'

  const user = await getUser(userId, event)

  if (user.$isEmpty('minecraft') || !user.minecraft) {
    throw createError({ statusCode: 400, statusMessage: 'Minecraft has not been linked' })
  }

  const minecraftProfile: any = await $fetch(`https://sessionserver.mojang.com/session/minecraft/profile/${user.minecraft.uuid}`)

  try {
    user.minecraft = { uuid: user.minecraft.uuid, username: minecraftProfile.name }

    await applyUsername(user)
  } catch (e) {
    console.error('Failed to update document', e)

    throw createError('Failed to update document')
  }

  return user.minecraft
})
