<?php
/**
 * HVAC Manage Event Handler
 * 
 * Ensures proper shortcode processing for the manage-event page
 * 
 * @package HVAC_Community_Events
 * @since 1.0.0
 */

if (!defined('ABSPATH')) {
    exit;
}

class HVAC_Manage_Event {
    
    /**
     * Constructor
     */
    public function __construct() {
        // Hook into content filter with low priority (99) to run after other filters
        add_filter('the_content', array($this, 'ensure_shortcode_processing'), 99);
        
        // Add authentication check for manage-event page
        add_action('template_redirect', array($this, 'check_manage_event_auth'));
        
        // Debug: Log when this class is instantiated
        if (defined('HVAC_CE_PLUGIN_DIR') && class_exists('HVAC_Logger')) {
            HVAC_Logger::info('HVAC_Manage_Event class instantiated', 'ManageEvent');
        }
    }
    
    /**
     * Ensure shortcode processing for manage-event page
     */
    public function ensure_shortcode_processing($content) {
        // Only process on manage-event page
        if (!is_page('manage-event')) {
            return $content;
        }
        
        // Debug logging
        if (class_exists('HVAC_Logger')) {
            HVAC_Logger::info('Processing manage-event page content', 'ManageEvent');
            HVAC_Logger::info('Original content: ' . substr($content, 0, 200), 'ManageEvent');
        }
        
        // Process all shortcodes in the content
        $processed_content = do_shortcode($content);
        
        // Debug: Log if content changed
        if (class_exists('HVAC_Logger') && $processed_content !== $content) {
            HVAC_Logger::info('Content was processed by do_shortcode', 'ManageEvent');
        }
        
        // Add navigation bar to the content
        $navigation_html = $this->get_navigation_bar();
        
        // If shortcode wasn't processed (plugin might be inactive), show helpful message
        if (strpos($processed_content, '[tribe_community_events') !== false) {
            if (class_exists('HVAC_Logger')) {
                HVAC_Logger::warning('tribe_community_events shortcode not processed - plugin may be inactive', 'ManageEvent');
            }
            
            $error_content = '<div class="hvac-notice hvac-error">
                <p><strong>Event Submission Form Unavailable</strong></p>
                <p>The event submission form is currently unavailable. Please ensure:</p>
                <ul>
                    <li>The Events Calendar plugin is active</li>
                    <li>The Events Calendar Community Events add-on is active</li>
                    <li>You are logged in as a trainer</li>
                </ul>
                <p><a href="' . esc_url(home_url('/hvac-dashboard/')) . '" class="button">Return to Dashboard</a></p>
            </div>
            <style>
                .hvac-notice.hvac-error {
                    background: #f8d7da;
                    border: 1px solid #f5c6cb;
                    color: #721c24;
                    padding: 20px;
                    margin: 20px 0;
                    border-radius: 4px;
                }
                .hvac-notice.hvac-error ul {
                    margin: 15px 0 15px 30px;
                }
            </style>';
            
            return $navigation_html . $error_content;
        }
        
        // Wrap the form content with navigation and styling
        $final_content = $navigation_html . '
        <div class="hvac-manage-event-content">
            ' . $processed_content . '
        </div>
        
        <style>
        .hvac-manage-event-content {
            margin-top: 30px;
        }
        
        /* Style the TEC Community Events form */
        .tribe-community-events-form {
            background: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            margin-bottom: 30px;
        }
        
        .tribe-community-events-form .tribe-events-page-title {
            color: #333;
            margin-bottom: 20px;
            padding-bottom: 15px;
            border-bottom: 2px solid #eee;
        }
        
        /* Form field styling */
        .tribe-community-events-form .tribe-events-form-row {
            margin-bottom: 20px;
        }
        
        .tribe-community-events-form label {
            font-weight: 600;
            color: #333;
            display: block;
            margin-bottom: 8px;
        }
        
        .tribe-community-events-form input[type="text"],
        .tribe-community-events-form input[type="email"],
        .tribe-community-events-form input[type="url"],
        .tribe-community-events-form textarea,
        .tribe-community-events-form select {
            width: 100%;
            padding: 12px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
            transition: border-color 0.3s ease;
        }
        
        .tribe-community-events-form input:focus,
        .tribe-community-events-form textarea:focus,
        .tribe-community-events-form select:focus {
            outline: none;
            border-color: #007cba;
            box-shadow: 0 0 5px rgba(0, 124, 186, 0.3);
        }
        
        /* Submit button styling */
        .tribe-community-events-form input[type="submit"],
        .tribe-community-events-form .tribe-events-submit {
            background: #007cba;
            color: white;
            padding: 12px 30px;
            border: none;
            border-radius: 4px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        
        .tribe-community-events-form input[type="submit"]:hover,
        .tribe-community-events-form .tribe-events-submit:hover {
            background: #005a87;
        }
        
        /* TinyMCE editor styling */
        .tribe-community-events-form .wp-editor-wrap {
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        
        /* Date picker styling */
        .tribe-community-events-form .tribe-datetime-block {
            background: #f9f9f9;
            padding: 15px;
            border-radius: 4px;
            margin: 10px 0;
        }
        
        /* Venue fields styling */
        .tribe-community-events-form .tribe-events-venue-form {
            background: #f9f9f9;
            padding: 20px;
            border-radius: 4px;
            margin: 15px 0;
        }
        
        /* Error and success messages */
        .tribe-community-events-form .tribe-events-notices {
            padding: 15px;
            margin: 20px 0;
            border-radius: 4px;
        }
        
        .tribe-community-events-form .tribe-events-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .tribe-community-events-form .tribe-events-success {
            background: #d1e7dd;
            color: #0f5132;
            border: 1px solid #badbcc;
        }
        </style>';
        
        return $final_content;
    }
    
    /**
     * Generate the navigation bar HTML
     */
    private function get_navigation_bar() {
        // Check if Help System is available for tooltips
        $help_available = class_exists('HVAC_Help_System');
        
        $nav_html = '
        <div class="hvac-dashboard-header">
            <h1 class="entry-title">Create Event</h1>
            <div class="hvac-dashboard-nav">';
        
        // Dashboard link
        if ($help_available) {
            $nav_html .= HVAC_Help_System::add_tooltip(
                '<a href="' . esc_url(home_url('/hvac-dashboard/')) . '" class="ast-button ast-button-secondary">Dashboard</a>',
                'Return to your main dashboard to view stats and manage events'
            );
        } else {
            $nav_html .= '<a href="' . esc_url(home_url('/hvac-dashboard/')) . '" class="ast-button ast-button-secondary">Dashboard</a>';
        }
        
        // Generate Certificates link
        if ($help_available) {
            $nav_html .= HVAC_Help_System::add_tooltip(
                '<a href="' . esc_url(home_url('/generate-certificates/')) . '" class="ast-button ast-button-primary">Generate Certificates</a>',
                'Create professional certificates for attendees who completed your training'
            );
        } else {
            $nav_html .= '<a href="' . esc_url(home_url('/generate-certificates/')) . '" class="ast-button ast-button-primary">Generate Certificates</a>';
        }
        
        // Certificate Reports link
        if ($help_available) {
            $nav_html .= HVAC_Help_System::add_tooltip(
                '<a href="' . esc_url(home_url('/certificate-reports/')) . '" class="ast-button ast-button-primary">Certificate Reports</a>',
                'View and manage all certificates you\'ve issued to attendees'
            );
        } else {
            $nav_html .= '<a href="' . esc_url(home_url('/certificate-reports/')) . '" class="ast-button ast-button-primary">Certificate Reports</a>';
        }
        
        // Trainer Profile link
        if ($help_available) {
            $nav_html .= HVAC_Help_System::add_tooltip(
                '<a href="' . esc_url(home_url('/trainer-profile/')) . '" class="ast-button ast-button-secondary">View Profile</a>',
                'Update your professional credentials, business information, and training specialties'
            );
        } else {
            $nav_html .= '<a href="' . esc_url(home_url('/trainer-profile/')) . '" class="ast-button ast-button-secondary">View Profile</a>';
        }
        
        // Help and Logout links
        $nav_html .= '
                <a href="' . esc_url(home_url('/hvac-documentation/')) . '" class="ast-button ast-button-secondary">Help</a>
                <a href="' . esc_url(wp_logout_url(home_url('/community-login/'))) . '" class="ast-button ast-button-secondary">Logout</a>
            </div>
        </div>';
        
        return $nav_html;
    }
    
    /**
     * Check authentication for manage-event page
     */
    public function check_manage_event_auth() {
        // Check if we're on the manage-event page
        if (is_page('manage-event') && !is_user_logged_in()) {
            // Redirect to login page
            wp_redirect(home_url('/community-login/?redirect_to=' . urlencode($_SERVER['REQUEST_URI'])));
            exit;
        }
    }
}