#!/bin/bash

echo "Testing login with timeout..."

# Test with timeout to avoid hanging
timeout 5 curl -s -X POST "http://localhost:8080/realms/b2b-flight-platform/protocol/openid-connect/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "client_id=b2b-flight-backend" \
  -d "client_secret=b2b-flight-backend-secret" \
  -d "grant_type=password" \
  -d "username=superadmin" \
  -d "password=SuperAdmin123!" \
  -d "scope=openid profile email"

EXIT_CODE=$?

if [ $EXIT_CODE -eq 124 ]; then
  echo ""
  echo "❌ Request timed out - Keycloak might be hanging"
  echo "This usually means the client is not configured for direct access grants"
elif [ $EXIT_CODE -eq 0 ]; then
  echo ""
  echo "✅ Request completed"
else
  echo ""
  echo "❌ Request failed with exit code: $EXIT_CODE"
fi
