#!/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 = "<!-- wp:group {\"className\":\"hvac-find-trainer-wrapper ast-container\"} -->
<div class=\"wp-block-group hvac-find-trainer-wrapper ast-container\">
    
    <!-- wp:group {\"className\":\"hvac-find-trainer-intro\"} -->
    <div class=\"wp-block-group hvac-find-trainer-intro\">
        <!-- wp:paragraph -->
        <p>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.</p>
        <!-- /wp:paragraph -->
    </div>
    <!-- /wp:group -->
    
    <!-- wp:columns {\"className\":\"hvac-map-filter-section\"} -->
    <div class=\"wp-block-columns hvac-map-filter-section\">
        
        <!-- wp:column {\"width\":\"66.66%\",\"className\":\"hvac-map-container\"} -->
        <div class=\"wp-block-column hvac-map-container\" style=\"flex-basis:66.66%\">
            <!-- wp:shortcode -->
            [display-map id=\"5872\"]
            <!-- /wp:shortcode -->
        </div>
        <!-- /wp:column -->
        
        <!-- wp:column {\"width\":\"33.33%\",\"className\":\"hvac-filter-sidebar\"} -->
        <div class=\"wp-block-column hvac-filter-sidebar\" style=\"flex-basis:33.33%\">
            <!-- wp:html -->
            <div class=\"hvac-filter-controls\">
                <input type=\"text\" class=\"hvac-search-input\" placeholder=\"Search trainers...\" aria-label=\"Search trainers\">
                <div class=\"hvac-filter-label\">Filters:</div>
                <button class=\"hvac-filter-button\" data-filter=\"state\" aria-label=\"Filter by State or Province\">
                    <span class=\"hvac-filter-icon\">▼</span> State / Province
                </button>
                <button class=\"hvac-filter-button\" data-filter=\"business_type\" aria-label=\"Filter by Business Type\">
                    <span class=\"hvac-filter-icon\">▼</span> Business Type
                </button>
                <button class=\"hvac-filter-button\" data-filter=\"training_format\" aria-label=\"Filter by Training Format\">
                    <span class=\"hvac-filter-icon\">▼</span> Training Format
                </button>
                <button class=\"hvac-filter-button\" data-filter=\"training_resources\" aria-label=\"Filter by Training Resources\">
                    <span class=\"hvac-filter-icon\">▼</span> Training Resources
                </button>
                <div class=\"hvac-active-filters\"></div>
            </div>
            <!-- /wp:html -->
        </div>
        <!-- /wp:column -->
        
    </div>
    <!-- /wp:columns -->
    
    <!-- wp:shortcode -->
    [hvac_trainer_directory]
    <!-- /wp:shortcode -->
    
    <!-- wp:group {\"className\":\"hvac-trainer-cta\"} -->
    <div class=\"wp-block-group hvac-trainer-cta\">
        <!-- wp:paragraph -->
        <p>Are you an HVAC Trainer that wants to be listed in our directory?</p>
        <!-- /wp:paragraph -->
        <!-- wp:buttons -->
        <div class=\"wp-block-buttons\">
            <!-- wp:button {\"className\":\"hvac-become-trainer-btn\"} -->
            <div class=\"wp-block-button hvac-become-trainer-btn\">
                <a class=\"wp-block-button__link\" href=\"/trainer-registration/\">Become a Trainer</a>
            </div>
            <!-- /wp:button -->
        </div>
        <!-- /wp:buttons -->
    </div>
    <!-- /wp:group -->
    
</div>
<!-- /wp:group -->";
    
    $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 ==="