#!/bin/bash

echo "=== Assigning All Page Templates ==="

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

# Function to set page template by path
set_template_by_path() {
    local page_path="$1"
    local template="$2"
    
    # Get page ID by path
    page_id=$(wp post list --post_type=page --post_name="$(basename "$page_path")" --post_parent=$(wp post list --post_type=page --post_name="$(dirname "$page_path" | sed 's|/|\n|g' | tail -1)" --field=ID --format=ids) --field=ID --format=ids | head -1)
    
    if [ -n "$page_id" ]; then
        echo "Setting template '$template' for page $page_path (ID: $page_id)..."
        wp post meta update "$page_id" "_wp_page_template" "$template"
    else
        echo "Page not found: $page_path"
    fi
}

# Trainer pages
set_template_by_path "training-login" "page-trainer-login.php"
set_template_by_path "trainer/dashboard" "page-trainer-dashboard.php"
set_template_by_path "trainer/registration" "page-trainer-registration.php"
set_template_by_path "trainer/my-profile" "page-trainer-profile.php"
set_template_by_path "trainer/event/manage" "page-manage-event.php"
set_template_by_path "trainer/event/summary" "page-event-summary.php"
set_template_by_path "trainer/certificate-reports" "page-certificate-reports.php"
set_template_by_path "trainer/generate-certificates" "page-generate-certificates.php"
set_template_by_path "trainer/email-attendees" "page-email-attendees.php"
set_template_by_path "trainer/communication-templates" "page-communication-templates.php"
set_template_by_path "trainer/communication-schedules" "page-communication-schedules.php"
set_template_by_path "trainer/documentation" "page-documentation.php"
set_template_by_path "trainer/attendee-profile" "page-attendee-profile.php"

# Master trainer pages
set_template_by_path "master-trainer/dashboard" "page-master-dashboard.php"
set_template_by_path "master-trainer/certificate-fix" "page-certificate-fix.php"
set_template_by_path "master-trainer/google-sheets" "page-google-sheets.php"

# Status pages
set_template_by_path "trainer-account-pending" "page-trainer-account-pending.php"
set_template_by_path "trainer-account-disabled" "page-trainer-account-disabled.php"
set_template_by_path "registration-pending" "page-registration-pending.php"

# Use simpler method for specific pages we know exist
echo -e "\nUsing direct ID assignment for known pages..."
wp post meta update 5331 "_wp_page_template" "page-trainer-login.php"
wp post meta update 5333 "_wp_page_template" "page-trainer-dashboard.php"
wp post meta update 5334 "_wp_page_template" "page-trainer-registration.php"
wp post meta update 5335 "_wp_page_template" "page-trainer-profile.php"
wp post meta update 5347 "_wp_page_template" "page-master-dashboard.php"

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

echo -e "\n✅ Templates assigned and cache cleared"
ENDSSH