#!/bin/bash

# Create HVAC Announcements pages on staging server

echo "🚀 Creating HVAC Announcements pages on staging..."

# Create the PHP script content
cat > /tmp/create-pages.php << 'EOF'
<?php
require_once('/home/974670.cloudwaysapps.com/uberrxmprk/public_html/wp-load.php');

echo "Creating HVAC Announcements pages...\n";

// Find parent pages
$master_parent = get_page_by_path('master-trainer');
$trainer_parent = get_page_by_path('trainer');

if (!$master_parent) {
    echo "❌ Error: master-trainer page not found\n";
    exit(1);
}
if (!$trainer_parent) {
    echo "❌ Error: trainer page not found\n";
    exit(1);
}

echo "✅ Found parent pages:\n";
echo "   - Master Trainer (ID: {$master_parent->ID})\n";
echo "   - Trainer (ID: {$trainer_parent->ID})\n\n";

// Check if pages already exist
$existing_manage = get_page_by_path('master-trainer/manage-announcements');
$existing_view = get_page_by_path('trainer/announcements');
$existing_resources = get_page_by_path('trainer/training-resources');

if ($existing_manage) {
    echo "⚠️  Manage Announcements page already exists (ID: {$existing_manage->ID})\n";
} else {
    // Create Manage Announcements page
    $manage_page = wp_insert_post(array(
        'post_title' => 'Manage Announcements',
        'post_name' => 'manage-announcements',
        'post_content' => '[hvac_announcements_manager]',
        'post_status' => 'publish',
        'post_type' => 'page',
        'post_parent' => $master_parent->ID,
    ));

    if ($manage_page && !is_wp_error($manage_page)) {
        update_post_meta($manage_page, '_wp_page_template', 'templates/page-master-manage-announcements.php');
        echo "✅ Created: /master-trainer/manage-announcements/ (ID: $manage_page)\n";
    } else {
        echo "❌ Failed to create manage announcements page\n";
        if (is_wp_error($manage_page)) {
            echo "   Error: " . $manage_page->get_error_message() . "\n";
        }
    }
}

if ($existing_view) {
    echo "⚠️  Announcements page already exists (ID: {$existing_view->ID})\n";
} else {
    // Create Announcements view page
    $view_page = wp_insert_post(array(
        'post_title' => 'Announcements',
        'post_name' => 'announcements',
        'post_content' => '[hvac_announcements_timeline]',
        'post_status' => 'publish',
        'post_type' => 'page',
        'post_parent' => $trainer_parent->ID,
    ));

    if ($view_page && !is_wp_error($view_page)) {
        update_post_meta($view_page, '_wp_page_template', 'templates/page-trainer-announcements.php');
        echo "✅ Created: /trainer/announcements/ (ID: $view_page)\n";
    } else {
        echo "❌ Failed to create announcements page\n";
        if (is_wp_error($view_page)) {
            echo "   Error: " . $view_page->get_error_message() . "\n";
        }
    }
}

if ($existing_resources) {
    echo "⚠️  Training Resources page already exists (ID: {$existing_resources->ID})\n";
} else {
    // Create Training Resources page
    $resources_page = wp_insert_post(array(
        'post_title' => 'Training Resources',
        'post_name' => 'training-resources', 
        'post_content' => '[hvac_google_drive_embed url="https://drive.google.com/drive/folders/1-G8gICMsih5E9YJ2FqaC5OqG0o4rwuSP"]',
        'post_status' => 'publish',
        'post_type' => 'page',
        'post_parent' => $trainer_parent->ID,
    ));

    if ($resources_page && !is_wp_error($resources_page)) {
        update_post_meta($resources_page, '_wp_page_template', 'templates/page-trainer-resources.php');
        echo "✅ Created: /trainer/training-resources/ (ID: $resources_page)\n";
    } else {
        echo "❌ Failed to create training resources page\n";
        if (is_wp_error($resources_page)) {
            echo "   Error: " . $resources_page->get_error_message() . "\n";
        }
    }
}

// Flush rewrite rules and clear cache
flush_rewrite_rules();
if (function_exists('wp_cache_flush')) {
    wp_cache_flush();
}

echo "\n🎉 HVAC Announcements pages setup complete!\n";
echo "\nPages should be available at:\n";
echo "- https://upskill-staging.measurequick.com/master-trainer/manage-announcements/\n";
echo "- https://upskill-staging.measurequick.com/trainer/announcements/\n";
echo "- https://upskill-staging.measurequick.com/trainer/training-resources/\n";
?>
EOF

# Get server details from existing deployment
SERVER_IP="146.190.76.204"
SERVER_USER="roodev"
SERVER_PATH="/home/974670.cloudwaysapps.com/uberrxmprk/public_html"

# Check if we can access the server (using the same method as deploy.sh)
echo "📡 Connecting to staging server..."

# Try to execute the PHP script
ssh -o ConnectTimeout=10 -o StrictHostKeyChecking=no ${SERVER_USER}@${SERVER_IP} "
    echo '🔧 Executing page creation script on server...'
    cd ${SERVER_PATH}
    php /tmp/create-pages.php
" 2>/dev/null || {
    echo "❌ SSH connection failed. You may need to run this manually on the staging server."
    echo "📋 To run manually, copy this script to the staging server and execute:"
    echo "   php /path/to/create-pages.php"
    exit 1
}

echo "✅ HVAC Announcements pages creation completed!"