#!/bin/bash

echo "🔍 Verifying Create Event Page Creation"
echo "======================================"

# Connect to staging and check if pages were actually created
ssh wp@upskill-staging.measurequick.com << 'EOF'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

echo "📋 Checking for 'trainer' parent page..."
wp post list --post_type=page --name=trainer --fields=ID,post_title,post_name,post_status

echo ""
echo "📋 Searching for 'create-event' pages..."
wp post list --post_type=page --s="create-event" --fields=ID,post_title,post_name,post_status,post_parent

echo ""
echo "📋 Searching for pages with 'create' in title..."
wp post list --post_type=page --s="create" --fields=ID,post_title,post_name,post_status,post_parent

echo ""
echo "📋 Checking all trainer sub-pages..."
TRAINER_ID=$(wp post list --post_type=page --name=trainer --field=ID)
if [ ! -z "$TRAINER_ID" ]; then
    echo "Trainer page ID: $TRAINER_ID"
    wp post list --post_type=page --post_parent=$TRAINER_ID --fields=ID,post_title,post_name,post_status
else
    echo "❌ Trainer parent page not found!"
fi

echo ""
echo "📋 Checking recent posts (last 10)..."
wp post list --post_type=page --orderby=date --order=DESC --posts_per_page=10 --fields=ID,post_title,post_name,date

echo ""
echo "🔧 Flushing rewrite rules..."
wp rewrite flush

echo ""
echo "🔧 Testing direct page access via ID..."
CREATE_EVENT_ID=$(wp post list --post_type=page --s="create-event" --field=ID | head -1)
if [ ! -z "$CREATE_EVENT_ID" ]; then
    echo "Create event page ID: $CREATE_EVENT_ID"
    echo "Direct URL test: /?page_id=$CREATE_EVENT_ID"
    wp post get $CREATE_EVENT_ID --fields=ID,post_title,post_name,post_status,post_content,meta
else
    echo "❌ No create-event page found in database"
fi

EOF

echo ""
echo "✅ Page verification completed!"