#!/bin/bash

echo "=== Creating Astra Child Theme for HVAC Community Events ==="

# SSH into staging and create child theme
ssh roodev@146.190.76.204 << 'EOF'
cd /home/974670.cloudwaysapps.com/uberrxmprk/public_html

# Create child theme directory
CHILD_THEME_DIR="wp-content/themes/astra-child-hvac"
echo "Creating child theme directory: $CHILD_THEME_DIR"
mkdir -p "$CHILD_THEME_DIR"

# Create style.css for child theme
cat > "$CHILD_THEME_DIR/style.css" << 'STYLE'
/*
Theme Name: Astra Child - HVAC Community Events
Theme URI: https://upskill.measurequick.com
Author: Ben Reed
Author URI: https://tealmaker.com
Description: Child theme for Astra to support HVAC Community Events plugin templates
Template: astra
Version: 1.0.0
License: GPL v2 or later
Text Domain: astra-child-hvac
*/

/* Import parent theme styles */
@import url("../astra/style.css");

/* Custom styles for HVAC Community Events can be added below */
STYLE

# Create functions.php for child theme
cat > "$CHILD_THEME_DIR/functions.php" << 'PHP'
<?php
/**
 * Astra Child Theme functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Astra-child-hvac
 * @since 1.0.0
 */

/**
 * Define Constants
 */
define( 'CHILD_THEME_ASTRA_CHILD_VERSION', '1.0.0' );

/**
 * Enqueue styles
 */
function child_enqueue_styles() {
    // Enqueue parent theme stylesheet
    wp_enqueue_style( 'astra-parent-theme-css', get_template_directory_uri() . '/style.css', array('astra-theme-css'), ASTRA_THEME_VERSION, 'all' );
}
add_action( 'wp_enqueue_scripts', 'child_enqueue_styles', 15 );

/**
 * Load HVAC plugin templates from child theme
 * This allows the plugin templates to work properly
 */
add_filter( 'theme_page_templates', function( $templates ) {
    // Add HVAC plugin page templates
    $hvac_templates = array(
        'page-trainer-dashboard.php' => 'Trainer Dashboard',
        'page-trainer-registration.php' => 'Trainer Registration',
        'page-master-dashboard.php' => 'Master Dashboard',
        'page-trainer-login.php' => 'Trainer Login',
        'page-trainer-profile.php' => 'Trainer Profile',
        'page-manage-event.php' => 'Manage Event',
        'page-event-summary.php' => 'Event Summary',
        'page-certificate-reports.php' => 'Certificate Reports',
        'page-generate-certificates.php' => 'Generate Certificates',
        'page-email-attendees.php' => 'Email Attendees',
        'page-communication-templates.php' => 'Communication Templates',
        'page-communication-schedules.php' => 'Communication Schedules',
        'page-documentation.php' => 'Documentation',
        'page-attendee-profile.php' => 'Attendee Profile',
        'page-google-sheets.php' => 'Google Sheets',
        'page-certificate-fix.php' => 'Certificate Fix',
        'page-trainer-account-pending.php' => 'Account Pending',
        'page-trainer-account-disabled.php' => 'Account Disabled',
        'page-registration-pending.php' => 'Registration Pending',
    );
    
    return array_merge( $templates, $hvac_templates );
}, 10, 1 );
PHP

# Create screenshot placeholder
echo "Creating screenshot placeholder..."
cat > "$CHILD_THEME_DIR/screenshot.png" << 'SCREENSHOT'
# This is a placeholder - replace with actual screenshot
SCREENSHOT

# Activate the child theme
echo "Activating child theme..."
wp theme activate astra-child-hvac

# Create directory for HVAC templates
mkdir -p "$CHILD_THEME_DIR/hvac-community-events"

# Copy templates from plugin to child theme
PLUGIN_DIR="wp-content/plugins/hvac-community-events"
if [ -d "$PLUGIN_DIR/templates" ]; then
    echo "Copying page templates to child theme..."
    find "$PLUGIN_DIR/templates" -name "page-*.php" -exec cp {} "$CHILD_THEME_DIR/" \;
    
    echo "Templates copied:"
    ls -la "$CHILD_THEME_DIR"/page-*.php
fi

# Clear cache
wp cache flush
echo "Child theme created and activated successfully!"

# Show theme info
wp theme get astra-child-hvac --fields=name,status,version
EOF