export default defineEventHandler(async (event) => {
  try {
    // Clear the session cookie
    deleteCookie(event, 'keycloak-session')
    
    console.log('[KEYCLOAK] User logged out, session cleared')
    
    // Redirect to Keycloak logout to clear SSO session
    const logoutUrl = 'https://auth.portnimara.dev/realms/client-portal/protocol/openid-connect/logout?' + 
      new URLSearchParams({
        redirect_uri: 'https://client.portnimara.dev/login'
      }).toString()
    
    await sendRedirect(event, logoutUrl)
  } catch (error) {
    console.error('[KEYCLOAK] Logout error:', error)
    throw createError({
      statusCode: 500,
      statusMessage: 'Logout failed'
    })
  }
})
