#!/bin/bash

echo "=== Listing and Fixing HVAC Pages ==="

ssh roodev@146.190.76.204 << 'ENDSSH'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

echo "=== Current HVAC Pages ==="
wp post list --post_type=page --meta_key=_wp_page_template --meta_value='hvac%' --fields=ID,post_title,post_name,post_content --format=table

echo -e "\n=== Searching for HVAC pages by title ==="
wp post list --post_type=page --s="trainer" --fields=ID,post_title,post_name,post_content --format=table

echo -e "\n=== Searching for master pages ==="
wp post list --post_type=page --s="master" --fields=ID,post_title,post_name,post_content --format=table

echo -e "\n=== Finding pages by URL path ==="
# Find trainer dashboard page
trainer_dash_id=$(wp db query "SELECT ID FROM wp_posts WHERE post_name='dashboard' AND post_parent=(SELECT ID FROM wp_posts WHERE post_name='trainer' AND post_type='page') AND post_type='page'" --skip-column-names)
if [ -n "$trainer_dash_id" ]; then
    echo "Found trainer dashboard page ID: $trainer_dash_id"
    wp post update "$trainer_dash_id" --post_content="[hvac_dashboard]"
    echo "✅ Updated trainer dashboard"
fi

# Find master dashboard page
master_dash_id=$(wp db query "SELECT ID FROM wp_posts WHERE post_name='dashboard' AND post_parent=(SELECT ID FROM wp_posts WHERE post_name='master-trainer' AND post_type='page') AND post_type='page'" --skip-column-names)
if [ -n "$master_dash_id" ]; then
    echo "Found master dashboard page ID: $master_dash_id"
    wp post update "$master_dash_id" --post_content="[hvac_master_dashboard]"
    echo "✅ Updated master dashboard"
fi

# Find registration page
registration_id=$(wp db query "SELECT ID FROM wp_posts WHERE post_name='registration' AND post_parent=(SELECT ID FROM wp_posts WHERE post_name='trainer' AND post_type='page') AND post_type='page'" --skip-column-names)
if [ -n "$registration_id" ]; then
    echo "Found registration page ID: $registration_id"
    wp post update "$registration_id" --post_content="[hvac_trainer_registration]"
    echo "✅ Updated registration page"
fi

# Clear cache
wp cache flush
wp breeze purge --cache=all

echo -e "\n✅ Script complete"
ENDSSH