#!/bin/bash

# Fix WebSocket Proxy Configuration on VPS
# Updates nginx config to point WebSocket to correct port (8052 instead of 8038)

echo "=== WebSocket Proxy Fix Deployment ==="
echo "Target: VPS 138.197.148.28"
echo "Fix: Update WebSocket proxy from port 8038 to 8052"
echo "=================================="

# Load environment variables
source .env

# Verify we have the required environment variables
if [ -z "$VPS_SSH_USER" ] || [ -z "$VPS_SSH_PASS" ] || [ -z "$VPS_IP" ]; then
    echo "❌ Missing VPS connection details in .env file"
    echo "Required: VPS_SSH_USER, VPS_SSH_PASS, VPS_IP"
    exit 1
fi

echo "🔧 Deploying WebSocket proxy fix..."

# Create the fix script for the VPS
cat << 'EOF' > fix-proxy-endpoints.sh
#!/bin/bash

echo "=== Fixing WebSocket Proxy Configuration ==="

# Backup current nginx configuration
sudo cp /etc/nginx/sites-available/ws.upskillhvac.measurequick.com /etc/nginx/sites-available/ws.upskillhvac.measurequick.com.backup.$(date +%Y%m%d_%H%M%S)

# Update the WebSocket proxy configuration
echo "Updating WebSocket proxy from port 8038 to 8052..."
sudo sed -i 's/proxy_pass http:\/\/192\.168\.10\.249:8038\/ws;/proxy_pass http:\/\/192\.168\.10\.249:8052\/ws;/g' /etc/nginx/sites-available/ws.upskillhvac.measurequick.com

# Test nginx configuration
echo "Testing nginx configuration..."
if sudo nginx -t; then
    echo "✅ Nginx configuration test passed"
    
    # Reload nginx
    echo "Reloading nginx..."
    sudo systemctl reload nginx
    
    if [ $? -eq 0 ]; then
        echo "✅ Nginx reloaded successfully"
        echo ""
        echo "=== WebSocket Proxy Fix Complete ==="
        echo "WebSocket endpoint now correctly points to port 8052"
        echo ""
        echo "You can test the WebSocket connection at:"
        echo "wss://ws.upskillhvac.measurequick.com/ws"
    else
        echo "❌ Failed to reload nginx"
        exit 1
    fi
else
    echo "❌ Nginx configuration test failed"
    echo "Restoring backup..."
    sudo cp /etc/nginx/sites-available/ws.upskillhvac.measurequick.com.backup.$(date +%Y%m%d)* /etc/nginx/sites-available/ws.upskillhvac.measurequick.com
    exit 1
fi

# Verify the change was applied
echo ""
echo "=== Verification ==="
echo "Current WebSocket proxy configuration:"
grep -n "proxy_pass.*ws" /etc/nginx/sites-available/ws.upskillhvac.measurequick.com || echo "No WebSocket proxy configuration found"

echo ""
echo "=== Service Status Check ==="
sudo systemctl status nginx --no-pager -l
EOF

# Upload the fix script to VPS
echo "📤 Uploading fix script to VPS..."
sshpass -p "$VPS_SSH_PASS" scp -o StrictHostKeyChecking=no fix-proxy-endpoints.sh $VPS_SSH_USER@$VPS_IP:~/

# Execute the fix script on VPS
echo "⚡ Executing WebSocket proxy fix on VPS..."
sshpass -p "$VPS_SSH_PASS" ssh -o StrictHostKeyChecking=no $VPS_SSH_USER@$VPS_IP "chmod +x ~/fix-proxy-endpoints.sh && ~/fix-proxy-endpoints.sh"

# Check if the fix was successful
if [ $? -eq 0 ]; then
    echo ""
    echo "🎉 WebSocket Proxy Fix Deployed Successfully!"
    echo ""
    echo "✅ Nginx configuration updated"
    echo "✅ WebSocket now points to port 8052"
    echo "✅ Service reloaded"
    echo ""
    echo "🔗 Test WebSocket connection:"
    echo "   wss://ws.upskillhvac.measurequick.com/ws"
    echo ""
    echo "🧪 You can verify the fix by checking if WebSocket"
    echo "   connections now work properly in your application."
else
    echo ""
    echo "❌ WebSocket proxy fix failed"
    echo "Check the VPS logs for more details"
    exit 1
fi

# Clean up local fix script
rm fix-proxy-endpoints.sh

echo ""
echo "🏁 WebSocket proxy fix deployment complete!"