#!/bin/bash

# Fix Dashboard Template Assignment
# This script ensures the dashboard page uses the correct page template

source .env

echo "=== Fixing Dashboard Template Assignment ==="
echo "Target: Staging Server"

# SSH into staging and update the page template
ssh -o StrictHostKeyChecking=no "${UPSKILL_STAGING_SSH_USER}@${UPSKILL_STAGING_IP}" << 'EOF'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

echo "Finding dashboard page..."
wp post list --post_type=page --name=dashboard --field=ID --path=/home/974670.cloudwaysapps.com/uberrxmprk/public_html

echo "Updating dashboard page template..."
# Find the page ID
PAGE_ID=$(wp post list --post_type=page --name=dashboard --field=ID --path=/home/974670.cloudwaysapps.com/uberrxmprk/public_html | head -1)

if [ ! -z "$PAGE_ID" ]; then
    echo "Found page ID: $PAGE_ID"
    
    # Update the template
    wp post meta update $PAGE_ID _wp_page_template templates/page-trainer-dashboard.php --path=/home/974670.cloudwaysapps.com/uberrxmprk/public_html
    
    echo "Template updated to: templates/page-trainer-dashboard.php"
    
    # Verify the update
    echo "Verifying template assignment..."
    wp post meta get $PAGE_ID _wp_page_template --path=/home/974670.cloudwaysapps.com/uberrxmprk/public_html
    
    # Clear cache
    echo "Clearing cache..."
    wp cache flush --path=/home/974670.cloudwaysapps.com/uberrxmprk/public_html
    
    echo "✅ Dashboard template fixed!"
else
    echo "❌ Dashboard page not found!"
fi

EOF

echo "=== Template Fix Complete ==="