#!/bin/bash

echo "=== Verifying Page Creation After Deployment ==="
echo "Date: $(date)"
echo

# SSH connection details
SERVER="146.190.76.204"
USER="roodev"

echo "🔍 Checking if registration-pending page exists..."

# Execute verification via SSH
ssh ${USER}@${SERVER} << 'EOF'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

echo "=== Page Creation Verification ==="

echo "🔍 Step 1: Check if registration-pending page exists in database..."
wp post list --post_type=page --name=registration-pending --fields=ID,post_title,post_name,post_status

echo
echo "🔍 Step 2: List all pages with 'registration' or 'pending' in the name..."
wp post list --post_type=page --search="registration" --fields=ID,post_title,post_name,post_status

echo
echo "🔍 Step 3: Check if training-login page exists..."
wp post list --post_type=page --name=training-login --fields=ID,post_title,post_name,post_status

echo
echo "🔍 Step 4: Create registration-pending page manually if missing..."
EXISTING_PAGE=$(wp post list --post_type=page --name=registration-pending --format=count)
if [ "$EXISTING_PAGE" -eq 0 ]; then
    echo "Page doesn't exist, creating it..."
    wp post create --post_type=page --post_title="Registration Pending" --post_name="registration-pending" --post_status=publish --post_content='<div class="hvac-registration-pending">
        <h2>Registration Submitted Successfully</h2>
        <p>Thank you for registering as an HVAC trainer. Your registration has been submitted and is currently being reviewed.</p>
        <p>You will receive an email notification once your account has been approved and activated.</p>
        <p>If you have any questions, please contact our support team.</p>
        <p><a href="/training-login/" class="button">Return to Login</a></p>
    </div>'
    echo "✅ Registration pending page created successfully"
else
    echo "✅ Page already exists"
fi

echo
echo "🔍 Step 5: Test URL accessibility..."
wp eval "
echo 'Testing URL: ' . home_url('/registration-pending/') . PHP_EOL;
\$response = wp_remote_get(home_url('/registration-pending/'));
if (is_wp_error(\$response)) {
    echo 'Error: ' . \$response->get_error_message() . PHP_EOL;
} else {
    echo 'HTTP Status: ' . wp_remote_retrieve_response_code(\$response) . PHP_EOL;
}
"

EOF

echo "✅ Page creation verification completed!"