#!/bin/bash

# Setup Test Data for Swagger Testing
# Creates test agency and gets authentication token

echo "=========================================="
echo "Setting Up Test Data"
echo "=========================================="
echo ""

APP_URL="http://localhost:8081"
KEYCLOAK_URL="http://localhost:8080"

# Step 1: Check if Keycloak is running
echo "Step 1: Checking Keycloak..."
KEYCLOAK_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$KEYCLOAK_URL")

if [ "$KEYCLOAK_STATUS" != "200" ]; then
    echo "⚠️  Keycloak is not accessible at $KEYCLOAK_URL"
    echo "   Please start Keycloak first: docker-compose up -d keycloak"
    echo ""
fi

# Step 2: Check if application is running
echo "Step 2: Checking Application..."
APP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" "$APP_URL/actuator/health")

if [ "$APP_STATUS" != "200" ]; then
    echo "❌ Application is not running"
    echo "   Please start the application: docker-compose up -d app"
    exit 1
fi

echo "✅ Application is running"
echo ""

# Step 3: Try to get a token (this will fail if Keycloak is not configured)
echo "Step 3: Attempting to get authentication token..."
echo ""

TOKEN_RESPONSE=$(curl -s -X POST \
  "$KEYCLOAK_URL/realms/b2b-flight-platform/protocol/openid-connect/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=password" \
  -d "client_id=b2b-flight-backend" \
  -d "client_secret=b2b-flight-backend-secret" \
  -d "username=agent@test.com" \
  -d "password=password123" 2>&1)

if echo "$TOKEN_RESPONSE" | grep -q "access_token"; then
    ACCESS_TOKEN=$(echo "$TOKEN_RESPONSE" | python3 -c "import sys, json; print(json.load(sys.stdin)['access_token'])" 2>/dev/null)
    echo "✅ Successfully obtained access token!"
    echo ""
    echo "Token (first 50 chars): ${ACCESS_TOKEN:0:50}..."
    echo ""
    echo "=========================================="
    echo "Use this token in Swagger UI:"
    echo "=========================================="
    echo ""
    echo "1. Open: http://localhost:8081/swagger-ui.html"
    echo "2. Click 'Authorize' button"
    echo "3. Enter: Bearer $ACCESS_TOKEN"
    echo "4. Click 'Authorize' then 'Close'"
    echo ""
    
    # Save token to file for easy access
    echo "$ACCESS_TOKEN" > /tmp/jwt_token.txt
    echo "Token saved to: /tmp/jwt_token.txt"
    echo ""
    
else
    echo "⚠️  Could not get authentication token"
    echo ""
    echo "Keycloak needs to be configured with test users."
    echo "Run the Keycloak setup script:"
    echo ""
    echo "  cd keycloak"
    echo "  ./setup-keycloak.sh"
    echo ""
    echo "Or configure Keycloak manually:"
    echo "  1. Go to: http://localhost:8080/admin"
    echo "  2. Login with admin/admin"
    echo "  3. Create realm: b2b-flight-platform"
    echo "  4. Create client: b2b-flight-backend"
    echo "  5. Create test users with roles"
    echo ""
fi

# Step 4: Generate test UUIDs for agencies
echo "=========================================="
echo "Test Data for Swagger"
echo "=========================================="
echo ""
echo "Use these values when testing in Swagger UI:"
echo ""
echo "agencyId (query parameter):"
echo "  123e4567-e89b-12d3-a456-426614174000"
echo ""
echo "Flight Search Request Body:"
cat << 'EOF'
{
  "origin": "MAD",
  "destination": "JFK",
  "departureDate": "2026-03-15",
  "returnDate": "2026-03-22",
  "adults": 1,
  "children": 0,
  "infants": 0,
  "cabinClass": "ECONOMY"
}
EOF
echo ""
echo ""
echo "=========================================="
echo "Quick Test Commands"
echo "=========================================="
echo ""
echo "Test Amadeus API directly:"
echo "  ./test-amadeus-simple.sh"
echo ""
echo "Test complete booking flow:"
echo "  ./demo-complete-booking.sh"
echo ""
echo "View Swagger UI:"
echo "  open http://localhost:8081/swagger-ui.html"
echo ""
echo "View API docs:"
echo "  curl http://localhost:8081/api-docs | python3 -m json.tool"
echo ""
echo "=========================================="
