#!/bin/bash

# Fix registration pending page content
# This script updates the page content to use the template system

echo "🔧 Fixing registration pending page content..."

# SSH to production server
ssh upskill@207.154.230.172 << 'EOF'
cd /var/www/html

# Get the page ID
PAGE_ID=$(wp post list --post_type=page --name=registration-pending --field=ID --allow-root)

if [ -z "$PAGE_ID" ]; then
    echo "❌ Registration pending page not found"
    exit 1
fi

echo "📄 Found page ID: $PAGE_ID"

# Delete the existing page so it can be recreated with proper content
wp post delete $PAGE_ID --force --allow-root

echo "🗑️  Deleted existing page"

# Trigger page recreation by calling the plugin activation hook
wp eval 'HVAC_Page_Manager::create_pages();' --allow-root

echo "🔄 Recreated pages with updated content"

# Clear all caches
wp cache flush --allow-root
wp eval 'if (function_exists("wp_cache_flush")) wp_cache_flush();' --allow-root

# Clear Breeze cache if available
if wp plugin is-active breeze --allow-root; then
    wp eval 'if (class_exists("Breeze_Admin")) { Breeze_Admin::breeze_clear_all_cache(); }' --allow-root
    echo "🧹 Cleared Breeze cache"
fi

# Clear any OPcache
if command -v php >/dev/null 2>&1; then
    php -r "if (function_exists('opcache_reset')) { opcache_reset(); echo 'Cleared OPcache\n'; }"
fi

echo "✅ Registration pending page content updated successfully"
EOF

echo "🎉 Registration pending page fix completed!"