#!/bin/bash

# Update Find a Trainer page to use custom template
# Usage: ./scripts/update-find-trainer-template.sh

source .env

echo "=== Updating Find a Trainer Page Template ==="

ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" << 'ENDSSH'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

# Update the page to use the custom template
wp eval '
$page = get_page_by_path("find-a-trainer");
if ($page) {
    // Update to use custom template
    update_post_meta($page->ID, "_wp_page_template", "templates/page-find-trainer.php");
    
    // Clear the content since we are using a template
    wp_update_post([
        "ID" => $page->ID,
        "post_content" => ""
    ]);
    
    echo "Page updated to use custom template\n";
    echo "Page ID: " . $page->ID . "\n";
    echo "Template: " . get_post_meta($page->ID, "_wp_page_template", true) . "\n";
} else {
    echo "Find a Trainer page not found\n";
}
'

# Clear cache
wp cache flush
echo "Cache cleared"

ENDSSH

echo "=== Template update complete ==="

# Deploy the updated plugin with the new template
echo "=== Deploying updated plugin ==="
scripts/deploy.sh staging

echo "=== Update Complete ==="