#!/bin/bash

echo "=== Fixing Page Templates ==="

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

# Find and assign templates for pages without them
echo "Finding pages that need templates..."

# Certificate Reports
page_id=$(wp post list --post_type=page --fields=ID,post_title --format=csv | grep "Certificate Reports" | cut -d',' -f1)
if [ -n "$page_id" ]; then
    echo "Assigning template to Certificate Reports (ID: $page_id)"
    wp post meta update "$page_id" "_wp_page_template" "page-certificate-reports.php"
fi

# Generate Certificates
page_id=$(wp post list --post_type=page --fields=ID,post_title --format=csv | grep "Generate Certificates" | cut -d',' -f1)
if [ -n "$page_id" ]; then
    echo "Assigning template to Generate Certificates (ID: $page_id)"
    wp post meta update "$page_id" "_wp_page_template" "page-generate-certificates.php"
fi

# Email Attendees
page_id=$(wp post list --post_type=page --fields=ID,post_title --format=csv | grep "Email Attendees" | cut -d',' -f1)
if [ -n "$page_id" ]; then
    echo "Assigning template to Email Attendees (ID: $page_id)"
    wp post meta update "$page_id" "_wp_page_template" "page-email-attendees.php"
fi

# Communication Templates
page_id=$(wp post list --post_type=page --fields=ID,post_title --format=csv | grep "Communication Templates" | cut -d',' -f1)
if [ -n "$page_id" ]; then
    echo "Assigning template to Communication Templates (ID: $page_id)"
    wp post meta update "$page_id" "_wp_page_template" "page-communication-templates.php"
fi

# Communication Schedules
page_id=$(wp post list --post_type=page --fields=ID,post_title --format=csv | grep "Communication Schedules" | cut -d',' -f1)
if [ -n "$page_id" ]; then
    echo "Assigning template to Communication Schedules (ID: $page_id)"
    wp post meta update "$page_id" "_wp_page_template" "page-communication-schedules.php"
fi

# Trainer Documentation
page_id=$(wp post list --post_type=page --fields=ID,post_title --format=csv | grep "Trainer Documentation" | cut -d',' -f1)
if [ -n "$page_id" ]; then
    echo "Assigning template to Trainer Documentation (ID: $page_id)"
    wp post meta update "$page_id" "_wp_page_template" "page-documentation.php"
fi

# Event Summary
page_id=$(wp post list --post_type=page --fields=ID,post_title --format=csv | grep "Event Summary" | cut -d',' -f1)
if [ -n "$page_id" ]; then
    echo "Assigning template to Event Summary (ID: $page_id)"
    wp post meta update "$page_id" "_wp_page_template" "page-event-summary.php"
fi

# Manage Event
page_id=$(wp post list --post_type=page --fields=ID,post_title --format=csv | grep "Manage Event" | cut -d',' -f1)
if [ -n "$page_id" ]; then
    echo "Assigning template to Manage Event (ID: $page_id)"
    wp post meta update "$page_id" "_wp_page_template" "page-manage-event.php"
fi

# Certificate Fix (Master Trainer)
page_id=$(wp post list --post_type=page --fields=ID,post_title --format=csv | grep "Certificate System Diagnostics" | cut -d',' -f1)
if [ -n "$page_id" ]; then
    echo "Assigning template to Certificate System Diagnostics (ID: $page_id)"
    wp post meta update "$page_id" "_wp_page_template" "page-certificate-fix.php"
fi

# Google Sheets Integration
page_id=$(wp post list --post_type=page --fields=ID,post_title --format=csv | grep "Google Sheets Integration" | cut -d',' -f1)
if [ -n "$page_id" ]; then
    echo "Assigning template to Google Sheets Integration (ID: $page_id)"
    wp post meta update "$page_id" "_wp_page_template" "page-google-sheets.php"
fi

# Attendee Profile
page_id=$(wp post list --post_type=page --fields=ID,post_title --format=csv | grep "Attendee Profile" | cut -d',' -f1)
if [ -n "$page_id" ]; then
    echo "Assigning template to Attendee Profile (ID: $page_id)"
    wp post meta update "$page_id" "_wp_page_template" "page-attendee-profile.php"
fi

# Account Status Pages
page_id=$(wp post list --post_type=page --fields=ID,post_title --format=csv | grep "Account Pending Approval" | cut -d',' -f1)
if [ -n "$page_id" ]; then
    echo "Assigning template to Account Pending Approval (ID: $page_id)"
    wp post meta update "$page_id" "_wp_page_template" "page-trainer-account-pending.php"
fi

page_id=$(wp post list --post_type=page --fields=ID,post_title --format=csv | grep "Account Access Restricted" | cut -d',' -f1)
if [ -n "$page_id" ]; then
    echo "Assigning template to Account Access Restricted (ID: $page_id)"
    wp post meta update "$page_id" "_wp_page_template" "page-trainer-account-disabled.php"
fi

page_id=$(wp post list --post_type=page --fields=ID,post_title --format=csv | grep "Registration Pending" | cut -d',' -f1)
if [ -n "$page_id" ]; then
    echo "Assigning template to Registration Pending (ID: $page_id)"
    wp post meta update "$page_id" "_wp_page_template" "page-registration-pending.php"
fi

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

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

# List all pages with their templates
echo -e "\n=== Current Page Templates ==="
wp db query "SELECT p.ID, p.post_title, p.post_name, pm.meta_value as template FROM wp_posts p LEFT JOIN wp_postmeta pm ON p.ID = pm.post_id AND pm.meta_key = '_wp_page_template' WHERE p.post_type='page' AND (p.post_title LIKE '%trainer%' OR p.post_title LIKE '%certificate%' OR p.post_title LIKE '%email%' OR p.post_title LIKE '%communication%' OR p.post_title LIKE '%event%' OR p.post_title LIKE '%profile%' OR p.post_title LIKE '%google%' OR p.post_title LIKE '%account%' OR p.post_title LIKE '%registration%') ORDER BY p.ID"
ENDSSH