#!/bin/bash

# Templates to update
templates=(
    "templates/page-trainer-dashboard.php"
    "templates/page-trainer-organizer-manage.php"
    "templates/page-trainer-organizers-list.php"
    "templates/page-trainer-profile-edit.php"
    "templates/page-trainer-profile-view.php"
    "templates/page-trainer-profile.php"
    "templates/page-trainer-venue-manage.php"
)

# Header include code
header_include='    <?php 
    // Include trainer header with navigation and breadcrumbs
    $plugin_dir = WP_PLUGIN_DIR . '"'"'/hvac-community-events/'"'"';
    if (file_exists($plugin_dir . '"'"'templates/template-parts/trainer-header.php'"'"')) {
        include $plugin_dir . '"'"'templates/template-parts/trainer-header.php'"'"';
    }
    ?>'

for template in "${templates[@]}"; do
    if [ -f "$template" ]; then
        echo "Updating $template..."
        
        # Check if template already has the header include
        if grep -q "trainer-header.php" "$template"; then
            echo "  Already has header include, skipping..."
            continue
        fi
        
        # Create temporary file
        temp_file=$(mktemp)
        
        # Process the file
        awk -v header="$header_include" '
        /<div class="hvac-page-wrapper/ {
            print $0
            print header
            next
        }
        {print}
        ' "$template" > "$temp_file"
        
        # Replace original file
        mv "$temp_file" "$template"
        echo "  ✓ Updated"
    fi
done

echo "All templates updated!"