#!/bin/bash
# Create Find a Trainer page on staging
# Usage: ./scripts/create-find-trainer-page.sh
source .env
echo "=== Creating Find a Trainer Page on Staging ==="
# SSH into staging and create the page
ssh -o StrictHostKeyChecking=no "$UPSKILL_STAGING_SSH_USER@$UPSKILL_STAGING_IP" << 'ENDSSH'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html
# Create the Find a Trainer page via WP-CLI
wp eval '
// Initialize Find a Trainer page
if (class_exists("HVAC_Find_Trainer_Page")) {
    $finder = HVAC_Find_Trainer_Page::get_instance();
    $finder->register_page();
    echo "Find a Trainer page initialization triggered\n";
}
// Also directly create the page if it doesnt exist
$page = get_page_by_path("find-a-trainer");
if (!$page) {
    $page_content = "
    
    
    
        
        Find certified HVAC trainers in your area. Use the interactive map and filters below to discover trainers who match your specific needs. Click on any trainer to view their profile and contact them directly.
        
     
    
    
    
    
        
        
        
            
            [display-map id=\"5872\"]
            
        
        
        
        
        
        
        
     
    
    
    
    [hvac_trainer_directory]
    
    
    
    
        
        Are you an HVAC Trainer that wants to be listed in our directory?
        
        
        
        
     
    
    
 
";
    
    $page_data = [
        "post_title"    => "Find a Trainer",
        "post_name"     => "find-a-trainer",
        "post_content"  => $page_content,
        "post_status"   => "publish",
        "post_type"     => "page",
        "post_author"   => 1,
        "meta_input"    => [
            "_wp_page_template" => "default",
            "ast-site-content-layout" => "page-builder",
            "site-post-title" => "disabled",
            "site-sidebar-layout" => "no-sidebar",
            "ast-main-header-display" => "enabled",
            "ast-hfb-above-header-display" => "disabled",
            "ast-hfb-below-header-display" => "disabled",
            "ast-featured-img" => "disabled"
        ]
    ];
    
    $page_id = wp_insert_post($page_data);
    
    if ($page_id && !is_wp_error($page_id)) {
        update_option("hvac_find_trainer_page_id", $page_id);
        echo "Find a Trainer page created successfully (ID: $page_id)\n";
    } else {
        echo "Error creating Find a Trainer page\n";
    }
} else {
    echo "Find a Trainer page already exists\n";
}
'
# Create database table
wp eval '
if (class_exists("HVAC_Contact_Submissions_Table")) {
    HVAC_Contact_Submissions_Table::create_table();
    echo "Contact submissions table created\n";
} else {
    echo "HVAC_Contact_Submissions_Table class not found\n";
}
'
# Flush rewrite rules
wp rewrite flush
echo "Rewrite rules flushed"
# Clear cache
wp cache flush
echo "Cache cleared"
ENDSSH
echo "=== Verifying Page Creation ==="
# Check if page is accessible
HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" https://upskill-staging.measurequick.com/find-a-trainer/)
if [ "$HTTP_CODE" = "200" ]; then
    echo "✅ Find a Trainer page is accessible (HTTP $HTTP_CODE)"
    echo "URL: https://upskill-staging.measurequick.com/find-a-trainer/"
else
    echo "⚠️ Find a Trainer page returned HTTP $HTTP_CODE"
fi
echo "=== Setup Complete ==="