#!/bin/bash

echo "🔍 Removing TEC template overrides from staging"
echo "=============================================="

source .env

# SSH and remove the template override
sshpass -p "$STAGING_SSH_PASSWORD" ssh -o StrictHostKeyChecking=no roodev@$UPSKILL_STAGING_IP << 'EOF' 2>/dev/null | grep -v "Notice:"
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

echo "Checking for TEC template overrides..."

# Check in active theme
ACTIVE_THEME=$(wp theme list --status=active --field=name)
echo "Active theme: $ACTIVE_THEME"

# Remove from theme directories
if [ -f "wp-content/themes/$ACTIVE_THEME/tribe-events/community/edit-event.php" ]; then
    echo "Found override in active theme, removing..."
    rm -f "wp-content/themes/$ACTIVE_THEME/tribe-events/community/edit-event.php"
    echo "✅ Removed from active theme"
fi

if [ -f "wp-content/themes/astra-child-hvac/tribe-events/community/edit-event.php" ]; then
    echo "Found override in astra-child-hvac, removing..."
    rm -f "wp-content/themes/astra-child-hvac/tribe-events/community/edit-event.php"
    echo "✅ Removed from astra-child-hvac"
fi

# Also check parent theme
if [ -f "wp-content/themes/astra/tribe-events/community/edit-event.php" ]; then
    echo "Found override in astra parent, removing..."
    rm -f "wp-content/themes/astra/tribe-events/community/edit-event.php"
    echo "✅ Removed from astra parent"
fi

# Clear all caches
echo "Clearing caches..."
wp cache flush
wp rewrite flush

echo "✅ Template overrides removed and caches cleared"

EOF

echo "✅ Complete"