#!/bin/bash

echo "🔧 Fixing page templates on staging..."

# Set the correct templates for each page
sshpass -p "uSCO6f1y" ssh -o StrictHostKeyChecking=no roodev@146.190.76.204 << 'ENDSSH'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

echo "Setting page templates..."

# Trainer pages
wp post meta update 5655 _wp_page_template templates/page-trainer-venues-list.php 2>/dev/null
echo "✅ Set template for venue list (5655)"

wp post meta update 5656 _wp_page_template templates/page-trainer-venue-manage.php 2>/dev/null
echo "✅ Set template for venue manage (5656)"

# Find and update organizer manage page
ORG_ID=$(wp post list --post_type=page --name=manage --post_parent=$(wp post list --post_type=page --name=organizer --field=ID | head -1) --field=ID 2>/dev/null | head -1)
if [ ! -z "$ORG_ID" ]; then
    wp post meta update $ORG_ID _wp_page_template templates/page-trainer-organizer-manage.php 2>/dev/null
    echo "✅ Set template for organizer manage ($ORG_ID)"
fi

# Find and update training leads page
LEADS_ID=$(wp post list --post_type=page --name=training-leads --post_parent=$(wp post list --post_type=page --name=profile --field=ID | head -1) --field=ID 2>/dev/null | head -1)
if [ ! -z "$LEADS_ID" ]; then
    wp post meta update $LEADS_ID _wp_page_template templates/page-trainer-training-leads.php 2>/dev/null
    echo "✅ Set template for training leads ($LEADS_ID)"
fi

# Master trainer pages
MASTER_PARENT=$(wp post list --post_type=page --name=master-trainer --field=ID 2>/dev/null | head -1)

if [ ! -z "$MASTER_PARENT" ]; then
    # Google Sheets
    GS_ID=$(wp post list --post_type=page --name=google-sheets --post_parent=$MASTER_PARENT --field=ID 2>/dev/null | head -1)
    if [ ! -z "$GS_ID" ]; then
        wp post meta update $GS_ID _wp_page_template templates/page-master-google-sheets.php 2>/dev/null
        echo "✅ Set template for google sheets ($GS_ID)"
    fi
    
    # Announcements
    ANN_ID=$(wp post list --post_type=page --name=announcements --post_parent=$MASTER_PARENT --field=ID 2>/dev/null | head -1)
    if [ ! -z "$ANN_ID" ]; then
        wp post meta update $ANN_ID _wp_page_template templates/page-master-announcements.php 2>/dev/null
        echo "✅ Set template for announcements ($ANN_ID)"
    fi
    
    # Pending Approvals
    PA_ID=$(wp post list --post_type=page --name=pending-approvals --post_parent=$MASTER_PARENT --field=ID 2>/dev/null | head -1)
    if [ ! -z "$PA_ID" ]; then
        wp post meta update $PA_ID _wp_page_template templates/page-master-pending-approvals.php 2>/dev/null
        echo "✅ Set template for pending approvals ($PA_ID)"
    fi
    
    # Trainers
    TR_ID=$(wp post list --post_type=page --name=trainers --post_parent=$MASTER_PARENT --field=ID 2>/dev/null | head -1)
    if [ ! -z "$TR_ID" ]; then
        wp post meta update $TR_ID _wp_page_template templates/page-master-trainers.php 2>/dev/null
        echo "✅ Set template for trainers ($TR_ID)"
    fi
fi

# Clear caches
wp cache flush 2>/dev/null
echo "✅ Cache flushed"

# Flush rewrite rules
wp rewrite flush 2>/dev/null
echo "✅ Rewrite rules flushed"

ENDSSH

echo "✅ Page templates fixed!"
