<?php
/**
 * HVAC Event Edit Comprehensive Fix
 *
 * Comprehensive solution for TEC Community Events plugin bug where ALL event fields
 * (title, description, venue, organizer, categories, tags, meta fields) remain empty
 * when editing existing events. This class provides complete event data to JavaScript
 * for comprehensive field population.
 *
 * @package HVAC_Community_Events
 * @since 2.0.2
 */

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

/**
 * HVAC_Event_Edit_Comprehensive class
 */
class HVAC_Event_Edit_Comprehensive {
    
    /**
     * Instance
     * 
     * @var HVAC_Event_Edit_Comprehensive
     */
    private static $instance = null;
    
    /**
     * Get instance
     * 
     * @return HVAC_Event_Edit_Comprehensive
     */
    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_comprehensive_fix'));
    }
    
    /**
     * Enqueue comprehensive fix script on event management pages
     */
    public function enqueue_comprehensive_fix() {
        // 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 comprehensive event data
        $event_data = $this->get_comprehensive_event_data($event_id);
        if (!$event_data) {
            return; // No event found or insufficient permissions
        }

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

        // Localize script with comprehensive event data
        wp_localize_script('hvac-event-edit-comprehensive', 'hvac_event_comprehensive', array(
            'event_id' => $event_id,
            'event_data' => $event_data,
            'nonce' => wp_create_nonce('hvac_event_edit_' . $event_id),
            'debug' => defined('WP_DEBUG') && WP_DEBUG,
            'ajax_url' => admin_url('admin-ajax.php')
        ));

        // Enqueue CSS for visual feedback
        wp_enqueue_style(
            'hvac-event-edit-fixes',
            HVAC_PLUGIN_URL . 'assets/css/hvac-event-edit-fixes.css',
            array(),
            HVAC_PLUGIN_VERSION
        );

        HVAC_Logger::info("Comprehensive event edit fix enqueued for event ID: {$event_id}", 'Event Edit Comprehensive');
    }
    
    /**
     * 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 comprehensive event data for field population
     */
    private function get_comprehensive_event_data($event_id) {
        // Verify event exists and is a tribe event
        $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;
        }
        
        // Get comprehensive event data
        $data = array();
        
        // Core event data
        $data['core'] = array(
            'title' => sanitize_text_field($event->post_title),
            'content' => wp_kses_post($event->post_content),
            'excerpt' => sanitize_text_field($event->post_excerpt),
            'status' => sanitize_text_field($event->post_status)
        );
        
        // Event meta data
        $data['meta'] = $this->get_event_meta_data($event_id);
        
        // Venue data
        $data['venue'] = $this->get_venue_data($event_id);
        
        // Organizer data
        $data['organizer'] = $this->get_organizer_data($event_id);
        
        // Taxonomy data
        $data['taxonomies'] = $this->get_taxonomy_data($event_id);
        
        // Featured image
        $data['featured_image'] = $this->get_featured_image_data($event_id);
        
        // Additional TEC data
        $data['tec_data'] = $this->get_tec_specific_data($event_id);
        
        return $data;
    }
    
    /**
     * Get event meta data
     */
    private function get_event_meta_data($event_id) {
        $meta = array();
        
        // Get all post meta
        $all_meta = get_post_meta($event_id);
        
        // TEC-specific meta fields
        $tec_fields = array(
            '_EventStartDate',
            '_EventEndDate',
            '_EventStartTime',
            '_EventEndTime', 
            '_EventAllDay',
            '_EventTimezone',
            '_EventURL',
            '_EventCost',
            '_EventCurrencySymbol',
            '_EventCurrencyPosition',
            '_VirtualEvent',
            '_VirtualURL',
            '_EventShowMap',
            '_EventShowMapLink',
            '_EventRecurrence',
            '_EventDateTimeSeparator',
            '_EventTimeRangeSeparator'
        );
        
        foreach ($tec_fields as $field) {
            if (isset($all_meta[$field])) {
                $meta[$field] = sanitize_text_field($all_meta[$field][0]);
            }
        }
        
        return $meta;
    }
    
    /**
     * Get venue data
     */
    private function get_venue_data($event_id) {
        $venue_data = null;
        
        // Get venue ID using TEC function
        if (function_exists('tribe_get_venue_id')) {
            $venue_id = tribe_get_venue_id($event_id);
            if ($venue_id) {
                $venue = get_post($venue_id);
                if ($venue) {
                    $venue_meta = get_post_meta($venue_id);
                    
                    $venue_data = array(
                        'id' => $venue_id,
                        'title' => sanitize_text_field($venue->post_title),
                        'content' => wp_kses_post($venue->post_content),
                        'address' => isset($venue_meta['_VenueAddress']) ? sanitize_text_field($venue_meta['_VenueAddress'][0]) : '',
                        'city' => isset($venue_meta['_VenueCity']) ? sanitize_text_field($venue_meta['_VenueCity'][0]) : '',
                        'state' => isset($venue_meta['_VenueState']) ? sanitize_text_field($venue_meta['_VenueState'][0]) : '',
                        'province' => isset($venue_meta['_VenueProvince']) ? sanitize_text_field($venue_meta['_VenueProvince'][0]) : '',
                        'zip' => isset($venue_meta['_VenueZip']) ? sanitize_text_field($venue_meta['_VenueZip'][0]) : '',
                        'country' => isset($venue_meta['_VenueCountry']) ? sanitize_text_field($venue_meta['_VenueCountry'][0]) : '',
                        'phone' => isset($venue_meta['_VenuePhone']) ? sanitize_text_field($venue_meta['_VenuePhone'][0]) : '',
                        'url' => isset($venue_meta['_VenueURL']) ? esc_url($venue_meta['_VenueURL'][0]) : ''
                    );
                }
            }
        }
        
        return $venue_data;
    }
    
    /**
     * Get organizer data
     */
    private function get_organizer_data($event_id) {
        $organizer_data = null;
        
        // Get organizer ID using TEC function
        if (function_exists('tribe_get_organizer_id')) {
            $organizer_id = tribe_get_organizer_id($event_id);
            if ($organizer_id) {
                $organizer = get_post($organizer_id);
                if ($organizer) {
                    $organizer_meta = get_post_meta($organizer_id);
                    
                    $organizer_data = array(
                        'id' => $organizer_id,
                        'title' => sanitize_text_field($organizer->post_title),
                        'content' => wp_kses_post($organizer->post_content),
                        'phone' => isset($organizer_meta['_OrganizerPhone']) ? sanitize_text_field($organizer_meta['_OrganizerPhone'][0]) : '',
                        'website' => isset($organizer_meta['_OrganizerWebsite']) ? esc_url($organizer_meta['_OrganizerWebsite'][0]) : '',
                        'email' => isset($organizer_meta['_OrganizerEmail']) ? sanitize_email($organizer_meta['_OrganizerEmail'][0]) : ''
                    );
                }
            }
        }
        
        return $organizer_data;
    }
    
    /**
     * Get taxonomy data (categories, tags)
     */
    private function get_taxonomy_data($event_id) {
        $taxonomies = array();
        
        // Event categories
        $categories = wp_get_post_terms($event_id, 'tribe_events_cat', array('fields' => 'all'));
        if (!is_wp_error($categories)) {
            $taxonomies['categories'] = array();
            foreach ($categories as $category) {
                $taxonomies['categories'][] = array(
                    'id' => $category->term_id,
                    'name' => sanitize_text_field($category->name),
                    'slug' => sanitize_text_field($category->slug)
                );
            }
        }
        
        // Event tags  
        $tags = wp_get_post_terms($event_id, 'post_tag', array('fields' => 'all'));
        if (!is_wp_error($tags)) {
            $taxonomies['tags'] = array();
            foreach ($tags as $tag) {
                $taxonomies['tags'][] = array(
                    'id' => $tag->term_id,
                    'name' => sanitize_text_field($tag->name),
                    'slug' => sanitize_text_field($tag->slug)
                );
            }
        }
        
        return $taxonomies;
    }
    
    /**
     * Get featured image data
     */
    private function get_featured_image_data($event_id) {
        $featured_image = null;
        
        $image_id = get_post_thumbnail_id($event_id);
        if ($image_id) {
            $featured_image = array(
                'id' => $image_id,
                'url' => esc_url(wp_get_attachment_image_url($image_id, 'full')),
                'alt' => sanitize_text_field(get_post_meta($image_id, '_wp_attachment_image_alt', true))
            );
        }
        
        return $featured_image;
    }
    
    /**
     * Get TEC-specific data
     */
    private function get_tec_specific_data($event_id) {
        $tec_data = array();
        
        // Get recurring event data if applicable
        if (function_exists('tribe_is_recurring_event') && tribe_is_recurring_event($event_id)) {
            $tec_data['is_recurring'] = true;
            // Add recurring event specific data if needed
        } else {
            $tec_data['is_recurring'] = false;
        }
        
        // Get virtual event data
        if (function_exists('tribe_is_virtual_event')) {
            $tec_data['is_virtual'] = tribe_is_virtual_event($event_id);
        }
        
        return $tec_data;
    }
    
    /**
     * 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 can edit posts of this type
        if (current_user_can('edit_post', $event_id)) {
            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;
    }
    
    /**
     * AJAX handler for getting event data (if needed for dynamic loading)
     */
    public function ajax_get_event_data() {
        // Verify nonce
        if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'hvac_event_edit_' . intval($_POST['event_id']))) {
            wp_send_json_error('Invalid nonce');
        }
        
        $event_id = intval($_POST['event_id']);
        $event_data = $this->get_comprehensive_event_data($event_id);
        
        if ($event_data) {
            wp_send_json_success($event_data);
        } else {
            wp_send_json_error('Unable to load event data');
        }
    }
}