<?php
/**
 * HVAC Event Edit Fix
 *
 * Workaround for TEC Community Events plugin bug where title and description
 * fields remain empty when editing existing events. This class provides
 * JavaScript with the event data needed to populate empty fields.
 *
 * @package HVAC_Community_Events
 * @since 2.0.1
 */

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

/**
 * HVAC_Event_Edit_Fix class
 */
class HVAC_Event_Edit_Fix {
    
    /**
     * Instance
     * 
     * @var HVAC_Event_Edit_Fix
     */
    private static $instance = null;
    
    /**
     * Get instance
     * 
     * @return HVAC_Event_Edit_Fix
     */
    public static function instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    /**
     * Constructor
     */
    private function __construct() {
        add_action('wp_enqueue_scripts', array($this, 'enqueue_fix_script'));
    }
    
    /**
     * Enqueue the fix script on event management pages
     * 
     * DISABLED: Using proper TEC edit_event view instead of JavaScript workaround
     */
    public function enqueue_fix_script() {
        // DISABLED: Proper TEC solution implemented using edit_event view
        // JavaScript workaround no longer needed
        return;
        
        // Only load on event management pages
        if (!$this->is_event_manage_page()) {
            return;
        }

        // Get event ID from URL
        $event_id = $this->get_event_id_from_url();
        if (!$event_id) {
            return; // No event ID means new event creation, no fix needed
        }

        // Get event data
        $event_data = $this->get_event_data($event_id);
        if (!$event_data) {
            return; // No event found or insufficient data
        }

        // Enqueue the fix script
        wp_enqueue_script(
            'hvac-event-edit-fix',
            HVAC_PLUGIN_URL . 'assets/js/hvac-event-edit-fix.js',
            array('jquery'),
            HVAC_PLUGIN_VERSION,
            true
        );

        // Localize script with event data
        wp_localize_script('hvac-event-edit-fix', 'hvac_event_edit', array(
            'event_id' => $event_id,
            'event_title' => $event_data['title'],
            'event_content' => $event_data['content'],
            'debug' => defined('WP_DEBUG') && WP_DEBUG
        ));

        HVAC_Logger::info("Event edit fix script enqueued for event ID: {$event_id}", 'Event Edit Fix');
    }
    
    /**
     * Check if we're on an event management page
     */
    private function is_event_manage_page() {
        global $post;
        
        // Check if we're on the trainer/event/manage page
        if (is_page() && $post) {
            $page_slug = $post->post_name;
            $page_path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
            
            // Check various ways this page might be identified
            return (
                $page_slug === 'manage-event' ||
                strpos($page_path, 'trainer/event/manage') !== false ||
                strpos($page_path, 'manage-event') !== false
            );
        }
        
        return false;
    }
    
    /**
     * Get event ID from URL parameters
     */
    private function get_event_id_from_url() {
        if (isset($_GET['event_id']) && is_numeric($_GET['event_id'])) {
            return intval($_GET['event_id']);
        }
        
        return null;
    }
    
    /**
     * Get event data for the fix script
     */
    private function get_event_data($event_id) {
        $event = get_post($event_id);
        
        if (!$event || $event->post_type !== 'tribe_events') {
            return null;
        }
        
        // Verify user has permission to edit this event
        if (!$this->can_user_edit_event($event_id)) {
            return null;
        }
        
        return array(
            'title' => $event->post_title,
            'content' => $event->post_content
        );
    }
    
    /**
     * Check if current user can edit the event
     */
    private function can_user_edit_event($event_id) {
        $event = get_post($event_id);
        
        if (!$event) {
            return false;
        }
        
        // Allow if user is admin
        if (current_user_can('manage_options')) {
            return true;
        }
        
        // Allow if user is the event author
        if ($event->post_author == get_current_user_id()) {
            return true;
        }
        
        // Allow if user has trainer capabilities
        if (current_user_can('hvac_trainer') || current_user_can('hvac_master_trainer')) {
            return true;
        }
        
        return false;
    }
    
    /**
     * Log when the fix is applied (for debugging)
     */
    public static function log_fix_applied($event_id, $fields_fixed) {
        HVAC_Logger::info("Event edit fix applied to event ID {$event_id}, fields fixed: {$fields_fixed}", 'Event Edit Fix');
    }
}