#!/bin/bash

# Force Page Content Fix Script
# This script forces WordPress pages to have the correct shortcodes

echo "🔧 Forcing page content fix on staging..."

# SSH command to run on server
ssh roodev@146.190.76.204 << 'EOF'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

# Run WordPress CLI to update page content directly
wp eval '
// Define the page shortcode mappings
$page_shortcodes = array(
    "trainer/venue/list" => "[hvac_trainer_venues_list]",
    "trainer/venue/manage" => "[hvac_trainer_venue_manage]",
    "trainer/organizer/manage" => "[hvac_trainer_organizer_manage]",
    "trainer/profile/training-leads" => "[hvac_trainer_training_leads]",
    "master-trainer/announcements" => "[hvac_announcements_timeline]",
    "master-trainer/trainers" => "[hvac_master_trainers]",
    "master-trainer/pending-approvals" => "[hvac_pending_approvals]",
    "master-trainer/events" => "[hvac_master_events]",
    "master-trainer/google-sheets" => "[hvac_google_sheets]",
);

foreach ($page_shortcodes as $path => $shortcode) {
    $page = get_page_by_path($path);
    
    if ($page) {
        // Force update the page content
        $updated = wp_update_post(array(
            "ID" => $page->ID,
            "post_content" => $shortcode
        ));
        
        if ($updated) {
            echo "✅ Updated: " . $path . " with shortcode " . $shortcode . "\n";
        } else {
            echo "❌ Failed to update: " . $path . "\n";
        }
    } else {
        echo "⚠️  Page not found: " . $path . "\n";
    }
}

// Clear all caches
if (function_exists("wp_cache_flush")) {
    wp_cache_flush();
    echo "✅ Cache flushed\n";
}

// Force the Page Content Manager to run
if (class_exists("HVAC_Page_Content_Manager")) {
    $manager = HVAC_Page_Content_Manager::instance();
    $manager->force_fix_all_pages();
    echo "✅ Page Content Manager force fix executed\n";
} else {
    echo "⚠️  Page Content Manager class not found\n";
}
'

# Flush rewrite rules
wp rewrite flush
echo "✅ Rewrite rules flushed"

# Clear object cache
wp cache flush
echo "✅ Object cache cleared"

EOF

echo "✅ Page content fix complete!"