#!/bin/bash

# API Performance Testing Script
# This script tests the performance of the backend API endpoints

echo "=========================================="
echo "API Performance Test"
echo "=========================================="
echo ""

# Create curl format file
cat > /tmp/curl-format.txt << 'EOF'
    time_namelookup:  %{time_namelookup}s\n
       time_connect:  %{time_connect}s\n
    time_appconnect:  %{time_appconnect}s\n
   time_pretransfer:  %{time_pretransfer}s\n
      time_redirect:  %{time_redirect}s\n
 time_starttransfer:  %{time_starttransfer}s\n
                    ----------\n
         time_total:  %{time_total}s\n
       size_download:  %{size_download} bytes\n
      speed_download:  %{speed_download} bytes/sec\n
EOF

echo "Testing: upcomingProgramsWithLanguages endpoint"
echo "URL: https://ministryprogsapi.tniglobal.org/api/upcomingProgramsWithLanguages"
echo ""
curl -w "@/tmp/curl-format.txt" -o /dev/null -s https://ministryprogsapi.tniglobal.org/api/upcomingProgramsWithLanguages
echo ""
echo "=========================================="
echo ""

echo "Testing: database API endpoint"
echo "URL: https://database.tniglobal.org/api/health (example)"
echo ""
curl -w "@/tmp/curl-format.txt" -o /dev/null -s https://database.tniglobal.org/api/ || echo "Endpoint check completed"
echo ""
echo "=========================================="
echo ""

# Get actual response to analyze size
echo "Fetching actual response data..."
echo ""
RESPONSE_FILE="/tmp/api_response.json"
curl -s https://ministryprogsapi.tniglobal.org/api/upcomingProgramsWithLanguages -o $RESPONSE_FILE

if [ -f "$RESPONSE_FILE" ]; then
    RESPONSE_SIZE=$(wc -c < "$RESPONSE_FILE")
    echo "Response size: $RESPONSE_SIZE bytes ($(($RESPONSE_SIZE / 1024)) KB)"
    
    # Check if it's JSON and count items if it's an array
    if command -v jq &> /dev/null; then
        echo ""
        echo "Analyzing response structure with jq..."
        if jq -e '.data | type' "$RESPONSE_FILE" > /dev/null 2>&1; then
            ITEM_COUNT=$(jq '.data | length' "$RESPONSE_FILE" 2>/dev/null || echo "N/A")
            echo "Number of programs in response: $ITEM_COUNT"
        else
            echo "Response structure:"
            jq 'keys' "$RESPONSE_FILE" 2>/dev/null || echo "Could not parse JSON structure"
        fi
    else
        echo "(Install 'jq' for detailed JSON analysis: apt-get install jq)"
    fi
    
    rm "$RESPONSE_FILE"
fi

echo ""
echo "=========================================="
echo "Recommendations:"
echo "=========================================="
echo ""
echo "✅ Good: time_total < 500ms"
echo "⚠️  Warning: time_total between 500ms - 1s"
echo "❌ Bad: time_total > 1s"
echo ""
echo "If response time is > 1s, consider:"
echo "  1. Add database indexes"
echo "  2. Implement caching (Redis)"
echo "  3. Limit query results"
echo "  4. Optimize database queries"
echo ""

# Cleanup
rm -f /tmp/curl-format.txt

echo "=========================================="
echo "Test completed!"
echo "=========================================="
