export default defineEventHandler(async (event) => {
  console.log('[DEBUG] Testing Keycloak connectivity...')
  
  try {
    // Test basic connectivity to Keycloak
    const wellKnownUrl = 'https://auth.portnimara.dev/realms/client-portal/.well-known/openid-configuration'
    
    const response = await $fetch(wellKnownUrl, {
      retry: 0
    }) as any
    
    return {
      success: true,
      message: 'Keycloak connectivity successful',
      endpoint: wellKnownUrl,
      response: {
        issuer: response.issuer,
        authorization_endpoint: response.authorization_endpoint,
        token_endpoint: response.token_endpoint,
        userinfo_endpoint: response.userinfo_endpoint
      }
    }
  } catch (error: any) {
    console.error('[DEBUG] Keycloak connectivity test failed:', error)
    
    return {
      success: false,
      message: 'Keycloak connectivity failed',
      error: {
        message: error.message,
        status: error.status,
        cause: error.cause?.message
      }
    }
  }
})
