export default defineEventHandler((event) => {
  // Authentik forward-auth pasa información del usuario en headers
  const username = getHeader(event, 'x-authentik-username') || 'Usuario'
  const email = getHeader(event, 'x-authentik-email') || ''
  const name = getHeader(event, 'x-authentik-name') || ''
  const uid = getHeader(event, 'x-authentik-uid') || ''

  // Retornar información del usuario
  return {
    username,
    email,
    name: name || username, // Usar username si no hay name
    uid,
    initials: getInitials(name || username)
  }
})

// Helper para obtener iniciales
function getInitials(name: string): string {
  if (!name) return '?'

  const parts = name.trim().split(/\s+/)
  if (parts.length === 1) {
    return parts[0].substring(0, 2).toUpperCase()
  }

  return (parts[0][0] + parts[parts.length - 1][0]).toUpperCase()
}
