<?php
/**
 * HVAC Edit Event Shortcode
 *
 * Handles the [hvac_edit_event] shortcode for editing events
 *
 * @package HVAC_Community_Events
 * @since 1.0.0
 */

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

/**
 * HVAC_Edit_Event_Shortcode class
 */
class HVAC_Edit_Event_Shortcode {
    
    /**
     * Get instance
     */
    public static function instance() {
        static $instance = null;
        if (null === $instance) {
            $instance = new self();
        }
        return $instance;
    }
    
    /**
     * Constructor
     */
    public function __construct() {
        add_shortcode('hvac_edit_event', array($this, 'render_edit_event'));
    }
    
    /**
     * Render edit event shortcode
     * 
     * @param array $atts Shortcode attributes
     * @return string HTML output
     */
    public function render_edit_event($atts = array()) {
        // Check authentication
        if (!is_user_logged_in()) {
            return '<div class="hvac-error-notice"><p>You must be logged in to edit events.</p></div>';
        }
        
        // Check capabilities
        if (!current_user_can('hvac_trainer')) {
            return '<div class="hvac-error-notice"><p>You do not have permission to edit events.</p></div>';
        }
        
        // Get event ID from URL parameter
        $event_id = isset($_GET['event_id']) ? intval($_GET['event_id']) : 0;
        
        // Start output buffering
        ob_start();
        ?>
        
        <div class="hvac-edit-event-wrapper">
            <?php
            // Display trainer navigation menu and breadcrumbs
            if (class_exists('HVAC_Menu_System')) {
                echo '<div class="hvac-navigation-wrapper">';
                HVAC_Menu_System::instance()->render_trainer_menu();
                echo '</div>';
            }
            
            // Display breadcrumbs
            if (class_exists('HVAC_Breadcrumbs')) {
                echo '<div class="hvac-breadcrumbs-wrapper">';
                HVAC_Breadcrumbs::instance()->render();
                echo '</div>';
            }
            ?>
            
            <h1>Edit Event</h1>
            
            <?php if ($event_id > 0) : ?>
                <div class="hvac-form-notice">
                    <p>Editing Event ID: <?php echo esc_html($event_id); ?> - Full control over all fields including excerpt.</p>
                </div>
                
                <div class="hvac-page-content">
                    <?php
                    // Check if TEC Community Events is active
                    if (shortcode_exists('tribe_community_events')) {
                        // Render the TEC edit form with the event ID
                        echo do_shortcode('[tribe_community_events view="edit_event" id="' . $event_id . '"]');
                    } else {
                        echo '<div class="hvac-error-notice"><p>The Events Calendar Community Events plugin is required but not active.</p></div>';
                    }
                    ?>
                </div>
                
                <script>
                // Load form field injector and REST API enhancement for editing
                jQuery(document).ready(function($) {
                    console.log('[Edit Event Shortcode] Initializing for event <?php echo $event_id; ?>...');
                    
                    // Store event ID for REST API to use
                    window.hvacEditEventId = <?php echo $event_id; ?>;
                    console.log('[Edit Event Shortcode] Set window.hvacEditEventId =', window.hvacEditEventId);
                    
                    // First load the form field injector
                    $.getScript('<?php echo HVAC_PLUGIN_URL; ?>assets/js/hvac-tec-form-fields-injector.js')
                        .done(function() {
                            console.log('[Edit Event Shortcode] Form field injector loaded');
                        })
                        .fail(function() {
                            console.error('[Edit Event Shortcode] Failed to load form field injector');
                        });
                    
                    // Then load REST API enhancement
                    setTimeout(function() {
                        // Check if REST API script is loaded
                        if (typeof HVACRestEventSubmission !== 'undefined') {
                            console.log('[Edit Event Shortcode] REST API script already loaded');
                            HVACRestEventSubmission.init();
                        } else {
                            console.log('[Edit Event Shortcode] Loading REST API script...');
                            $.getScript('<?php echo HVAC_PLUGIN_URL; ?>assets/js/hvac-rest-api-event-submission.js')
                                .done(function() {
                                    console.log('[Edit Event Shortcode] REST API script loaded successfully');
                                    if (typeof HVACRestEventSubmission !== 'undefined') {
                                        HVACRestEventSubmission.init();
                                        console.log('[Edit Event Shortcode] REST API initialized for edit mode');
                                    }
                                })
                                .fail(function() {
                                    console.error('[Edit Event Shortcode] Failed to load REST API script');
                                });
                        }
                    }, 1000);
                });
                </script>
            <?php else : ?>
                <div class="hvac-error-notice">
                    <p>No event specified. Please select an event to edit.</p>
                </div>
                
                <div class="hvac-page-content">
                    <p><a href="<?php echo esc_url(home_url('/trainer/event/manage/')); ?>" class="button">Back to Event Management</a></p>
                </div>
            <?php endif; ?>
        </div>
        
        <style>
        .hvac-edit-event-wrapper {
            max-width: 1200px; 
            margin: 0 auto;
            padding: 20px;
        }

        .hvac-edit-event-wrapper h1 {
            color: #1a1a1a;
            font-size: 28px;
            margin-bottom: 20px;
        }

        .hvac-form-notice {
            background: #f0f7ff;
            border: 1px solid #0073aa;
            border-radius: 4px;
            padding: 12px;
            margin-bottom: 20px;
        }

        .hvac-form-notice p {
            margin: 0;
            color: #0073aa;
        }

        .hvac-error-notice {
            background: #fff5f5;
            border: 1px solid #dc3232;
            border-radius: 4px;
            padding: 12px;
            margin-bottom: 20px;
        }

        .hvac-error-notice p {
            margin: 0;
            color: #dc3232;
        }
        </style>
        
        <?php
        return ob_get_clean();
    }
}

// Initialize the shortcode
HVAC_Edit_Event_Shortcode::instance();