<?php
/**
 * Main plugin class for HVAC Community Events
 */

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

class HVAC_Community_Events {
	/**
	 * The single instance of the class
	 */
	private static $instance = null;

	/**
	 * Registration instance
	 */
	private $registration = null;

	/**
	 * TEC Field Processor instance (Phase 2)
	 */
	private $tec_field_processor = null;

	/**
	 * Main instance
	 */
	public static function instance() {
		if (is_null(self::$instance)) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Constructor
	 */
	public function __construct() {
	    $this->define_constants();
	    $this->includes();
	    $this->init_hooks();
	}

	/**
	 * Define constants
	 */
	private function define_constants() {
		// Additional constants can be defined here
	}

	/**
	 * Include required files
	 */
	private function includes() {
	    $files_to_include = [
	        'class-hvac-roles.php',
	        'class-hvac-registration.php',
	        'class-hvac-settings.php',
	        'community/class-login-handler.php',
	        'community/class-event-handler.php',
	        'class-hvac-dashboard-data.php',
	        'class-hvac-master-dashboard-data.php',
	        'class-hvac-master-events-overview.php', // Master trainer events overview
	        'class-hvac-trainer-status.php',     // Trainer status management
	        'class-hvac-access-control.php',     // Access control system
	        'class-hvac-approval-workflow.php',  // Approval workflow system
	        'class-event-form-handler.php',     // Add our form handler
	        'class-event-author-fixer.php',     // Fix event author assignment
	        'class-hvac-dashboard.php',         // New dashboard handler
            // 'class-hvac-manage-event.php',   // Moved to plugin.php to prevent double-loading
            'class-hvac-event-navigation.php',  // Event navigation shortcode
            'class-hvac-event-manage-header.php', // Event management page header
            'class-hvac-help-system.php',       // Help system for tooltips and documentation
	        'certificates/class-certificate-installer.php',  // Certificate database installer
	        'certificates/class-certificate-manager.php',    // Certificate management
	        'certificates/class-certificate-generator.php',  // Certificate generation
	        'certificates/class-certificate-settings.php',   // Certificate settings
	        'certificates/class-certificate-template.php',   // Certificate template
	        'certificates/class-certificate-security.php',   // Certificate security
	        'certificates/class-certificate-url-handler.php', // Certificate URL handler
	        'certificates/class-certificate-ajax-handler.php', // Certificate AJAX handling
	        'certificates/class-certificate-fix.php',        // Certificate diagnostic/fix tool
	        'community/class-email-debug.php',    // Email debugging tools
	        'certificates/test-rewrite-rules.php', // Rewrite rules testing (temporary)
	        'google-sheets/class-google-sheets-auth.php',     // Google Sheets authentication
	        'google-sheets/class-google-sheets-manager.php',  // Google Sheets management
	        'communication/class-communication-templates.php', // Email template management
	        'communication/class-communication-installer.php', // Communication system database installer
	        'communication/class-communication-schedule-manager.php', // Communication schedule manager
	        'communication/class-communication-trigger-engine.php', // Communication trigger engine
	        'communication/class-communication-logger.php',    // Communication logger
	        'communication/class-communication-scheduler.php', // Communication scheduler
	        'class-hvac-organizers.php',           // Training organizers management
	        'class-hvac-venues.php',               // Training venues management
	        'class-hvac-trainer-profile-manager.php', // Trainer profile management
	        'class-hvac-profile-sync-handler.php', // Profile synchronization
	        'class-hvac-geocoding-service.php',    // Geocoding service
	        'class-hvac-trainer-profile-settings.php', // Profile settings
	        'class-hvac-geocoding-ajax.php',       // Geocoding AJAX handler
	        'class-hvac-scripts-styles.php',       // Scripts and styles management
	        'class-hvac-shortcodes.php',           // Shortcodes management
	        
	        // TEC Field Processing System (Phase 2)
	        'tec-fields/interface-hvac-tec-field-processor.php',        // Field processor interface
	        'tec-fields/class-hvac-tec-security-manager.php',           // Security framework
	        'tec-fields/class-hvac-tec-field-validator.php',            // Validation framework
	        'tec-fields/class-hvac-tec-field-processor.php',            // Main controller
	        'tec-fields/processors/class-hvac-tec-excerpt-processor.php',        // Excerpt processor
	        'tec-fields/processors/class-hvac-tec-categories-processor.php',     // Categories processor
	        'tec-fields/processors/class-hvac-tec-featured-image-processor.php'  // Featured image processor
	    ];
	    // Make sure Login_Handler is loaded first for shortcode registration
	    $login_handler_path = HVAC_PLUGIN_DIR . 'includes/community/class-login-handler.php';
	    if (file_exists($login_handler_path)) {
	        require_once $login_handler_path;
	        HVAC_Logger::info("Included file: community/class-login-handler.php", 'Core');
	    }
	    
	    foreach ($files_to_include as $file) {
	        // Skip Login_Handler as we've already loaded it
	        if ($file === 'community/class-login-handler.php') {
	            continue;
	        }
	        
	        $path = HVAC_PLUGIN_DIR . 'includes/' . $file;
	        if (file_exists($path)) {
	            require_once $path;
	            HVAC_Logger::info("Included file: {$file}", 'Core');
	        } else {
	            HVAC_Logger::error("Failed to include file: {$file} - File not found", 'Core');
	        }
	    }
	    
	    // Note: Zoho admin interface will be loaded in init_zoho_admin() method during hooks initialization
	    
	    HVAC_Logger::info('All required files loaded', 'Core');
	}

	/**
	 * Initialize hooks
	 */
	private function init_hooks() {
		// Register activation/deactivation hooks
		// Note: These hooks are typically registered outside the class instance context
		// register_activation_hook(__FILE__, array($this, 'activate')); // This won't work correctly here
		// register_deactivation_hook(__FILE__, array($this, 'deactivate')); // This won't work correctly here

		// Initialize other hooks
		add_action('init', array($this, 'init'));

		// Template loading for custom pages
		add_filter('template_include', array($this, 'load_custom_templates'), 999);
		
		
		// Force content on edit event page
		add_filter('the_content', array($this, 'force_edit_event_content'), 1);
		
		// Cache invalidation hooks for master dashboard performance
		add_action('save_post', array($this, 'clear_master_dashboard_cache'), 10, 1);
		add_action('delete_post', array($this, 'clear_master_dashboard_cache'), 10, 1);
		add_action('user_register', array($this, 'clear_master_dashboard_cache_on_user_change'));
		add_action('deleted_user', array($this, 'clear_master_dashboard_cache_on_user_change'));
		
		// TEC Template Override Support - Enhanced Field Processing (Phase 2)
		add_action('tribe_events_community_before_event_save', array($this, 'process_all_tec_fields'));
		
		// Initialize TEC field processor system
		$this->init_tec_field_processor();
		
		// Authentication checks - DISABLED: All moved to centralized HVAC_Access_Control system
		// The following legacy auth checks are now handled by HVAC_Access_Control::check_page_access()
		// add_action('template_redirect', array($this, 'check_event_summary_auth'));
		// add_action('template_redirect', array($this, 'check_email_attendees_auth'));
		// add_action('template_redirect', array($this, 'check_certificate_pages_auth'));
		// add_action('template_redirect', array($this, 'check_master_dashboard_auth'));
		// add_action('template_redirect', array($this, 'check_google_sheets_auth'));
		add_action('template_redirect', array($this, 'ensure_registration_page_public'), 1);
		
		// Scripts and styles are now handled by HVAC_Scripts_Styles
		// add_action('wp_enqueue_scripts', array($this, 'enqueue_scripts'));
	} // End init_hooks
	
	/**
	 * Ensure registration page is always publicly accessible
	 */
	public function ensure_registration_page_public() {
		// List of pages that should be publicly accessible
		$public_pages = array(
			'trainer/registration',     // Registration form
			'registration-pending',     // Registration success/pending page
			'training-login',          // Login page itself
		);
		
		// If we're on any of these pages, ensure they're accessible
		if (is_page($public_pages)) {
			// Do not redirect to login - these pages should be public
			// This runs with priority 1, before other auth checks
			return;
		}
	}
	
	/**
	 * Check authentication for event summary page
	 */
	public function check_event_summary_auth() {
		// Check if we're on the event-summary page
		if (is_page('trainer/event/summary') && !is_user_logged_in()) {
			// Redirect to login page
			wp_redirect(home_url('/training-login/?redirect_to=' . urlencode($_SERVER['REQUEST_URI'])));
			exit;
		}
	}
	
	/**
	 * Check authentication for email attendees page
	 */
	public function check_email_attendees_auth() {
		// Check if we're on the email-attendees page
		if (is_page('trainer/email-attendees') && !is_user_logged_in()) {
			// Redirect to login page
			wp_redirect(home_url('/training-login/?redirect_to=' . urlencode($_SERVER['REQUEST_URI'])));
			exit;
		}
	}

	/**
	 * Check authentication for certificate pages
	 */
	public function check_certificate_pages_auth() {
		// Check if we're on certificate-related pages
		if ((is_page('trainer/certificate-reports') || is_page('trainer/generate-certificates')) && !is_user_logged_in()) {
			// Redirect to login page
			wp_redirect(home_url('/training-login/?redirect_to=' . urlencode($_SERVER['REQUEST_URI'])));
			exit;
		}
	}
	
	/**
	 * Check authentication for master dashboard page
	 */
	public function check_master_dashboard_auth() {
		// Check if we're on the master dashboard page
		if (is_page('master-trainer/master-dashboard') || is_page(5508)) {
			if (!is_user_logged_in()) {
				// Redirect to login page
				wp_redirect(home_url('/training-login/?redirect_to=' . urlencode($_SERVER['REQUEST_URI'])));
				exit;
			}
			
			// Check if user has master dashboard permissions - check for role and admin
			$user = wp_get_current_user();
			if (!in_array('hvac_master_trainer', $user->roles) && !current_user_can('manage_options')) {
				// Redirect to regular dashboard or show error
				wp_redirect(home_url('/trainer/dashboard/?error=access_denied'));
				exit;
			}
		}
	}
	
	/**
	 * Check authentication for Google Sheets page
	 */
	public function check_google_sheets_auth() {
		// Check if we're on the Google Sheets page - use multiple detection methods
		$is_google_sheets_page = false;
		
		// Add debug logging
		if (isset($_GET['hvac_debug']) && $_GET['hvac_debug'] === 'google_sheets') {
			error_log('HVAC Google Sheets Auth Check - Starting');
			error_log('Request URI: ' . (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : 'Not set'));
			global $post;
			if ($post) {
				error_log('Current post ID: ' . $post->ID);
				error_log('Current post name: ' . $post->post_name);
				error_log('Current post type: ' . $post->post_type);
			}
		}
		
		// Method 1: Check by page path/slug
		if (is_page('master-trainer/google-sheets') || is_page('google-sheets')) {
			$is_google_sheets_page = true;
			if (isset($_GET['hvac_debug'])) error_log('HVAC Google Sheets: Detected via is_page()');
		}
		
		// Method 2: Check by URL
		if (!$is_google_sheets_page && isset($_SERVER['REQUEST_URI'])) {
			$uri = $_SERVER['REQUEST_URI'];
			if (strpos($uri, '/master-trainer/google-sheets') !== false) {
				$is_google_sheets_page = true;
				if (isset($_GET['hvac_debug'])) error_log('HVAC Google Sheets: Detected via URL');
			}
		}
		
		// Method 3: Check if current page has Google Sheets shortcode
		global $post;
		if (!$is_google_sheets_page && $post && has_shortcode($post->post_content, 'hvac_google_sheets')) {
			$is_google_sheets_page = true;
			if (isset($_GET['hvac_debug'])) error_log('HVAC Google Sheets: Detected via shortcode');
		}
		
		// Only proceed if we're actually on the Google Sheets page
		if ($is_google_sheets_page) {
			// Prevent infinite redirect loops
			if (isset($_GET['hvac_redirect_check'])) {
				return; // Already redirected once, don't redirect again
			}
			
			if (!is_user_logged_in()) {
				// Add a parameter to prevent redirect loops
				$redirect_url = add_query_arg('hvac_redirect_check', '1', home_url('/training-login/?redirect_to=' . urlencode($_SERVER['REQUEST_URI'])));
				wp_redirect($redirect_url);
				exit;
			}
			
			// Check if user has master dashboard permissions (same as master dashboard)
			if (!current_user_can('view_master_dashboard') && !current_user_can('view_all_trainer_data') && !current_user_can('manage_options')) {
				// Add a parameter to prevent redirect loops
				$redirect_url = add_query_arg('hvac_redirect_check', '1', home_url('/trainer/dashboard/?error=access_denied'));
				wp_redirect($redirect_url);
				exit;
			}
		}
	}

	/**
	 * Plugin activation (Should be called statically or from the main plugin file context)
	 */
	public static function activate() {
		// Activation code here (e.g., page creation, role creation)
		// Note: This method might need to be moved or called differently
	}

	/**
	 * Plugin deactivation (Should be called statically or from the main plugin file context)
	 */
	public static function deactivate() {
		// Remove the hvac_trainer role
		require_once HVAC_PLUGIN_DIR . 'includes/class-hvac-roles.php'; // Ensure class is available
		HVAC_Roles::remove_hvac_trainer_role();
		HVAC_Logger::info('Deactivation completed: HVAC trainer role removed.', 'Core');
	}

	/**
	 * Initialize function (hooked on 'init')
	 */
	public function init() {
		// Initialize roles
		$this->init_roles();
		
		// Initialize settings (admin menu)
		$this->init_settings();
		
		// Initialize Zoho admin always (needed for AJAX)
		$this->init_zoho_admin();
		
		// Initialize Google Sheets integration (needed for AJAX)
		$this->init_google_sheets();
		
		// Initialize admin dashboard if in admin or handling AJAX
		if (is_admin() || (defined('DOING_AJAX') && DOING_AJAX)) {
			$this->init_admin_dashboard();
		}
		
		// Initialize forms first
		$this->init_forms();
		
		// Initialize shortcodes after forms are ready
		$this->init_shortcodes();
		
		// Initialize certificate AJAX handler
		if (class_exists('HVAC_Certificate_AJAX_Handler')) {
			HVAC_Certificate_AJAX_Handler::instance();
		}
		
		// Initialize certificate security
		if (class_exists('HVAC_Certificate_Security')) {
			HVAC_Certificate_Security::instance();
		}
		
		// Initialize certificate URL handler
		if (class_exists('HVAC_Certificate_URL_Handler')) {
			HVAC_Certificate_URL_Handler::instance();
		}
		
		// Add manual flush rewrite rules for admins (debugging)
		if (is_admin() && current_user_can('manage_options') && isset($_GET['hvac_flush_rewrite']) && $_GET['hvac_flush_rewrite'] === '1') {
			flush_rewrite_rules();
			wp_die('Rewrite rules flushed. <a href="' . admin_url() . '">Return to admin</a>');
		}
		
		// Initialize event form handler
		if (class_exists('HVAC_Community_Events\Event_Form_Handler')) {
			new \HVAC_Community_Events\Event_Form_Handler();
		}
		
		// Initialize manage event handler - moved to plugin.php to prevent double-instantiation
		// if (class_exists('HVAC_Manage_Event')) {
		//	new HVAC_Manage_Event();
		// }
		
		// Initialize dashboard handler
		if (class_exists('HVAC_Dashboard')) {
			new HVAC_Dashboard();
		}
		
		// Initialize help system (singleton)
		if (class_exists('HVAC_Help_System')) {
			HVAC_Help_System::instance();
		}
		
		// Initialize organizers management
		if (class_exists('HVAC_Organizers')) {
			new HVAC_Organizers();
		}
		
		// Initialize venues management
		if (class_exists('HVAC_Venues')) {
			new HVAC_Venues();
		}
		
		// Initialize trainer profile manager
		if (class_exists('HVAC_Trainer_Profile_Manager')) {
			HVAC_Trainer_Profile_Manager::get_instance();
		}
		
		// Initialize scripts and styles management
		if (class_exists('HVAC_Scripts_Styles')) {
			HVAC_Scripts_Styles::instance();
		}
		
		// Initialize shortcodes management
		if (class_exists('HVAC_Shortcodes')) {
			HVAC_Shortcodes::instance();
		}
		
		// Initialize communication system
		$this->init_communication_system();
		
		// Access control system initialized in main plugin class (HVAC_Plugin)
		
		// Initialize approval workflow
		if (class_exists('HVAC_Approval_Workflow')) {
			new HVAC_Approval_Workflow();
		}
	}

	/**
	 * Initialize roles
	 */
	private function init_roles() {
		$roles = new HVAC_Roles();
		// Note: Role creation is handled in the activate method or the class constructor
	}
	
	/**
	 * Initialize settings
	 */
	private function init_settings() {
		// Settings are already initialized in HVAC_Plugin
		// new HVAC_Settings();
		
		// Enhanced Settings is now initialized in HVAC_Plugin to avoid duplication
		// if ( file_exists( HVAC_PLUGIN_DIR . 'includes/admin/class-hvac-enhanced-settings.php' ) ) {
		// 	require_once HVAC_PLUGIN_DIR . 'includes/admin/class-hvac-enhanced-settings.php';
		// 	new HVAC_Enhanced_Settings();
		// }
	}
	
	/**
	 * Initialize Zoho admin
	 */
	private function init_zoho_admin() {
		// Zoho admin is now initialized in HVAC_Plugin to prevent duplicate initialization
		// This method is kept for backward compatibility but does nothing
	}
	
	/**
	 * Initialize admin dashboard
	 */
	private function init_admin_dashboard() {
		if (file_exists(HVAC_PLUGIN_DIR . 'includes/admin/class-admin-dashboard.php')) {
			require_once HVAC_PLUGIN_DIR . 'includes/admin/class-admin-dashboard.php';
			new HVAC_Admin_Dashboard();
		}
	}

	/**
	 * Initialize forms
	 */
	private function init_forms() {
		$this->registration = new HVAC_Registration();
		// Note: Form registration is handled in the class constructor
		
		// Initialize event navigation shortcode
		new HVAC_Event_Navigation();
	}

	/**
	 * Initialize shortcodes
	 */
	private function init_shortcodes() {
		// Note: Registration form shortcode is registered in HVAC_Registration constructor

		// Community login shortcode - initialize Login_Handler to register the shortcode
		new \HVAC_Community_Events\Community\Login_Handler();

		// Dashboard shortcodes moved to class-hvac-shortcodes.php for centralization
		// The shortcodes are now registered in the centralized shortcode manager
		
		// Add the event summary shortcode
		add_shortcode('hvac_event_summary', array($this, 'render_event_summary'));

		// Add trainer profile shortcode
		add_shortcode('hvac_trainer_profile', array($this, 'render_trainer_profile'));
		
		// Note: Edit profile shortcode is registered in HVAC_Registration constructor

		// Add email attendees shortcode
		add_shortcode('hvac_email_attendees', array($this, 'render_email_attendees'));

		// Add certificate reports shortcode
		add_shortcode('hvac_certificate_reports', array($this, 'render_certificate_reports'));

		// Add generate certificates shortcode
		add_shortcode('hvac_generate_certificates', array($this, 'render_generate_certificates'));
		
		// Certificate fix shortcode is handled by the Certificate Fix class
		// to avoid duplicate registration and missing method issues
		
		// Add Google Sheets admin shortcode
		add_shortcode('hvac_google_sheets', array($this, 'render_google_sheets_admin'));
		
		// Add communication templates shortcode
		add_shortcode('hvac_communication_templates', array($this, 'render_communication_templates'));
		
		// Add communication schedules shortcode
		add_shortcode('hvac_communication_schedules', array($this, 'render_communication_schedules'));

		// Removed shortcode override - let The Events Calendar Community Events handle this shortcode
		// add_shortcode('tribe_community_events', array($this, 'render_tribe_community_events'));

		// Add future shortcodes here
	}

	/**
	 * Render dashboard content
	 */
	public function render_dashboard() {
		// Add debug comment
		$debug = '<!-- HVAC_Community_Events::render_dashboard called -->';
		
		if (!is_user_logged_in()) {
			return $debug . '<p>Please log in to view the dashboard.</p>';
		}

		// Include the dashboard template
		ob_start();
		echo $debug;
		include HVAC_PLUGIN_DIR . 'templates/template-hvac-dashboard.php';
		return ob_get_clean();
	}
	
	/**
	 * Render master dashboard content
	 */
	public function render_master_dashboard() {
		
		if (!is_user_logged_in()) {
			return '<p>Please log in to view the master dashboard.</p>';
		}
		
		// Check if user has master dashboard permissions - check roles instead of capabilities
		$user = wp_get_current_user();
		if (!in_array('hvac_master_trainer', $user->roles) && !current_user_can('manage_options')) {
			return '<div class="hvac-error">You do not have permission to view the master dashboard. This dashboard is only available to Master Trainers and Administrators.</div>';
		}

		
		// Include the master dashboard template
		ob_start();
		include HVAC_PLUGIN_DIR . 'templates/template-hvac-master-dashboard.php';
		return ob_get_clean();
	}
	
	/**
	 * Render event summary content
	 */
	public function render_event_summary() {
		// Check if user is logged in
		if (!is_user_logged_in()) {
			return '<p>Please log in to view the event summary.</p>';
		}
		
		// Get event ID from URL parameter
		$event_id = isset($_GET['event_id']) ? absint($_GET['event_id']) : 0;
		
		if ($event_id <= 0) {
			return '<div class="hvac-error">No event ID provided. Please access this page from your dashboard.</div>';
		}
		
		// Check if the event exists and user has permission to view it
		$event = get_post($event_id);
		if (!$event || get_post_type($event) !== Tribe__Events__Main::POSTTYPE) {
			return '<div class="hvac-error">Event not found or invalid.</div>';
		}
		
		// Check if the current user has permission to view this event
		// For now, we'll check if they're the post author or have edit_posts capability
		if ($event->post_author != get_current_user_id() && !current_user_can('edit_posts')) {
			return '<div class="hvac-error">You do not have permission to view this event summary.</div>';
		}
		
		// Include the event summary template
		ob_start();
		include HVAC_PLUGIN_DIR . 'templates/event-summary/template-event-summary.php';
		return ob_get_clean();
	}
	
	/**
	 * Render trainer profile content
	 */
	public function render_trainer_profile() {
		if (!is_user_logged_in()) {
			return '<p>Please log in to view your profile.</p>';
		}

		// Use the new HVAC_Trainer_Profile_Manager system if available
		if (class_exists('HVAC_Trainer_Profile_Manager')) {
			$profile_manager = HVAC_Trainer_Profile_Manager::get_instance();
			return $profile_manager->render_profile_view();
		}

		// Fallback to old template (for backward compatibility)
		ob_start();
		include HVAC_PLUGIN_DIR . 'templates/template-trainer-profile.php';
		return ob_get_clean();
	}
	
	/**
	 * Render email attendees content
	 */
	public function render_email_attendees() {
		// Check if user is logged in
		if (!is_user_logged_in()) {
			return '<p>Please log in to email event attendees.</p>';
		}
		
		// Get event ID from URL parameter
		$event_id = isset($_GET['event_id']) ? absint($_GET['event_id']) : 0;
		
		if ($event_id <= 0) {
			return '<div class="hvac-error">No event ID provided. Please access this page from your dashboard or event summary page.</div>';
		}
		
		// Check if the event exists and user has permission to view it
		$event = get_post($event_id);
		if (!$event || get_post_type($event) !== Tribe__Events__Main::POSTTYPE) {
			return '<div class="hvac-error">Event not found or invalid.</div>';
		}
		
		// Check if the current user has permission to view this event
		// For now, we'll check if they're the post author or have edit_posts capability
		if ($event->post_author != get_current_user_id() && !current_user_can('edit_posts')) {
			return '<div class="hvac-error">You do not have permission to email attendees for this event.</div>';
		}
		
		// Include the email attendees template
		ob_start();
		include HVAC_PLUGIN_DIR . 'templates/email-attendees/template-email-attendees.php';
		return ob_get_clean();
	}

	/**
	 * Validate template file for common syntax errors
	 * @param string $file_path Path to the template file
	 * @return bool|string True if valid, error message otherwise
	 */
	private function validate_template_file($file_path) {
		// Check if the file exists
		if (!file_exists($file_path)) {
			return "Template file does not exist: {$file_path}";
		}

		// Get file contents
		$content = file_get_contents($file_path);

		// Check for common syntax errors with escaped operators
		$common_errors = [
			'\\!==', // Escaped !==
			'\\!=' , // Escaped !=
			'\\!' , // Escaped !
			'\\=' , // Escaped =
			'\\>' , // Escaped >
			'\\<' , // Escaped <
		];

		foreach ($common_errors as $error) {
			if (strpos($content, $error) !== false) {
				// Find the line number
				$lines = explode("\n", $content);
				$line_number = 0;
				foreach ($lines as $i => $line) {
					if (strpos($line, $error) !== false) {
						$line_number = $i + 1;
						break;
					}
				}
				
				// Log the error for admin
				$filename = basename($file_path);
				HVAC_Logger::error("Syntax error detected in {$filename} line {$line_number}: Improper escape sequence {$error}", 'Core');
				
				return "Template file contains syntax errors: {$filename} line {$line_number}. Please contact the administrator.";
			}
		}

		return true;
	}

	/**
	 * Render certificate reports content
	 */
	public function render_certificate_reports() {
		// Check if user is logged in
		if (!is_user_logged_in()) {
			return '<p>Please log in to view certificate reports.</p>';
		}
		
		// Check if the current user has permission to view certificate reports
		// For now, we'll check if they're a trainer or have edit_posts capability
		if (!current_user_can('hvac_trainer') && !current_user_can('edit_posts')) {
			return '<div class="hvac-error">You do not have permission to view certificate reports.</div>';
		}
		
		// Make sure certificate manager is loaded
		require_once HVAC_PLUGIN_DIR . 'includes/certificates/class-certificate-manager.php';
		
		// Make sure certificate security is loaded
		require_once HVAC_PLUGIN_DIR . 'includes/certificates/class-certificate-security.php';
		
		// Validate the template file before including
		$template_path = HVAC_PLUGIN_DIR . 'templates/certificates/template-certificate-reports.php';
		$validation = $this->validate_template_file($template_path);
		if ($validation !== true) {
			return '<div class="hvac-error">' . esc_html($validation) . '</div>';
		}
		
		// Include the certificate reports template with error handling
		try {
			ob_start();
			include $template_path;
			return ob_get_clean();
		} catch (Exception $e) {
			return '<div class="hvac-error">Error loading certificate reports: ' . esc_html($e->getMessage()) . '</div>';
		}
	}

	/**
	 * Render generate certificates content
	 */
	public function render_generate_certificates() {
		// Check if user is logged in
		if (!is_user_logged_in()) {
			return '<p>Please log in to generate certificates.</p>';
		}
		
		// Get event ID from URL parameter if available
		$event_id = isset($_GET['event_id']) ? absint($_GET['event_id']) : 0;
		
		// Check if the event exists and user has permission to view it when event_id is provided
		if ($event_id > 0) {
			$event = get_post($event_id);
			if (!$event || get_post_type($event) !== Tribe__Events__Main::POSTTYPE) {
				return '<div class="hvac-error">Event not found or invalid.</div>';
			}
			
			// Check if the current user has permission to view this event
			if ($event->post_author != get_current_user_id() && !current_user_can('edit_posts')) {
				return '<div class="hvac-error">You do not have permission to generate certificates for this event.</div>';
			}
		} else {
			// If no event ID is provided, check general permissions
			if (!current_user_can('hvac_trainer') && !current_user_can('edit_posts')) {
				return '<div class="hvac-error">You do not have permission to generate certificates.</div>';
			}
		}
		
		// Make sure certificate manager is loaded
		require_once HVAC_PLUGIN_DIR . 'includes/certificates/class-certificate-manager.php';
		
		// Make sure certificate security is loaded
		require_once HVAC_PLUGIN_DIR . 'includes/certificates/class-certificate-security.php';
		
		// Make sure certificate generator is loaded
		require_once HVAC_PLUGIN_DIR . 'includes/certificates/class-certificate-generator.php';
		
		// Validate the template file before including
		$template_path = HVAC_PLUGIN_DIR . 'templates/certificates/template-generate-certificates.php';
		$validation = $this->validate_template_file($template_path);
		if ($validation !== true) {
			return '<div class="hvac-error">' . esc_html($validation) . '</div>';
		}
		
		// Include the generate certificates template with error handling
		try {
			ob_start();
			include $template_path;
			return ob_get_clean();
		} catch (Exception $e) {
			return '<div class="hvac-error">Error loading certificate generation: ' . esc_html($e->getMessage()) . '</div>';
		}
	}

	/**
	 * Include custom templates for plugin pages
	 */
	public function load_custom_templates($template) {
		// Debug logging for edit page
		if (strpos($_SERVER['REQUEST_URI'], '/trainer/event/edit') !== false) {
			error_log("HVAC Debug: load_custom_templates called for edit page");
			error_log("HVAC Debug: Original template: " . $template);
			error_log("HVAC Debug: Current page ID: " . get_the_ID());
			error_log("HVAC Debug: is_page(): " . (is_page() ? 'YES' : 'NO'));
		}
		
		$custom_template = null;
		
		
		// Check for trainer dashboard page - force correct template
		// Fix: Use get_page_by_path for hierarchical URLs as is_page() doesn't work with paths
		$trainer_dashboard_page = get_page_by_path('trainer/dashboard');
		if ($trainer_dashboard_page && is_page($trainer_dashboard_page->ID)) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/page-trainer-dashboard.php';
		}
		
		// For master dashboard, force our template regardless of WordPress template assignment
		// Fix: Use get_page_by_path for hierarchical URLs as is_page() doesn't work with paths
		$master_dashboard_page = get_page_by_path('master-trainer/master-dashboard');
		if ($master_dashboard_page && is_page($master_dashboard_page->ID)) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/page-master-dashboard.php';
		}
		
		// Check for google-sheets page
		// Fix: Use get_page_by_path for hierarchical URLs
		$google_sheets_page = get_page_by_path('master-trainer/google-sheets');
		if ($google_sheets_page && is_page($google_sheets_page->ID)) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/template-google-sheets.php';
		}
		
		// Check for community-login page
		if (is_page('training-login')) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/page-community-login.php';
		}

		
		// Check for trainer-profile page
		if (is_page('trainer/my-profile')) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/template-trainer-profile.php';
		}
		
		// Check for trainer registration page
		$current_path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
		if ($current_path === 'trainer/registration' || is_page('trainer-registration') || is_page('registration')) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/page-trainer-registration.php';
		}
		
		// Check for new trainer profile page
		if (is_page('trainer/profile')) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/page-trainer-profile.php';
		}
		
		// Check for edit-event page
		if (is_page('trainer/edit-event')) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/page-edit-event.php';
			HVAC_Logger::info("Loading edit-event template", 'Template Loader');
		}
		
		// NOTE: Custom edit event page is now handled by HVAC_Custom_Event_Edit class
		// to avoid duplicate template loading logic
		
		// Check for event-summary page
		if (is_page('trainer/event/summary')) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/template-event-summary.php';
		}
		
		// Check for manage-event page
		if (is_page('trainer/event/manage')) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/page-manage-event.php';
		}
		
		// Check for email-attendees page
		if (is_page('trainer/email-attendees')) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/email-attendees/template-email-attendees.php';
		}

		// Check for certificate-reports page
		if (is_page('trainer/certificate-reports')) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/page-certificate-reports.php';
		}

		// Check for generate-certificates page
		if (is_page('trainer/generate-certificates')) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/page-generate-certificates.php';
		}
		
		// Check for edit-profile page
		if (is_page('edit-profile')) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/template-edit-profile.php';
		}
		
		// Check for communication-templates page
		if (is_page('trainer/communication-templates')) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/communication/template-communication-templates.php';
		}
		
		// Check for master trainer communication-templates page
		if (is_page('master-trainer/communication-templates')) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/page-master-communication-templates.php';
		}

		// Check for single event view (temporary)
		if (is_singular('tribe_events')) {
			$custom_template = HVAC_PLUGIN_DIR . 'templates/single-tribe_events.php';
		}
		
		// Process the custom template if one was found
		if ($custom_template !== null) {
			// Check if file exists
			if (file_exists($custom_template)) {
				// Validate certificate templates for syntax errors
				if (strpos($custom_template, 'certificates/') !== false) {
					$validation = $this->validate_template_file($custom_template);
					if ($validation !== true) {
						// Log error and show friendly message
						HVAC_Logger::error("Template validation failed in load_custom_templates: {$validation}", 'Core');
						
						// Display error or fallback template
						add_action('wp_footer', function() use ($validation) {
							echo '<div class="hvac-error" style="position:fixed;bottom:10px;left:10px;z-index:100000;background:#fff;padding:10px;border:1px solid #e0e0e0;">' . 
								esc_html('There was a problem loading this page. The administrator has been notified. Please try again later.') . '</div>';
						});
						
						// Either fall back to the standard WP template or use a simple error template
						return $template;
					}
				}
				
				// Template exists and passes validation
				return $custom_template;
			}
		}

		return $template;
	} // End load_custom_templates
	
	
	/**
	 * Force edit event content on edit event page
	 */
	public function force_edit_event_content($content) {
		// Check if we're on the edit event page
		if ((is_page(6177) || strpos($_SERVER['REQUEST_URI'], '/trainer/event/edit') !== false) && in_the_loop() && is_main_query()) {
			// Check if user is logged in
			if (!is_user_logged_in()) {
				wp_safe_redirect(home_url('/training-login/?redirect=' . urlencode($_SERVER['REQUEST_URI'])));
				exit;
			}
			
			// Get event ID from URL
			$event_id = isset($_GET['event_id']) ? (int) $_GET['event_id'] : 0;
			
			// Load and return the custom form
			ob_start();
			?>
			<!-- Custom Event Edit Form Injected -->
			<div class="hvac-event-edit-wrapper">
				<h1>Edit Event</h1>
				<p>Event ID: <?php echo esc_html($event_id); ?></p>
				<p>This is a test to confirm the content injection is working.</p>
				<p>If you see this, the template loading mechanism is working but needs the full form implementation.</p>
			</div>
			<?php
			return ob_get_clean();
		}
		return $content;
	}

	// REMOVED: render_tribe_community_events() method
	// This method was overriding The Events Calendar Community Events shortcode
	// Let TEC handle the shortcode properly instead
	
	/**
	 * Render Google Sheets admin content
	 */
	public function render_google_sheets_admin() {
		if (!is_user_logged_in()) {
			return '<p>Please log in to access Google Sheets integration.</p>';
		}
		
		// Check if user has master dashboard permissions
		if (!current_user_can('view_master_dashboard') && !current_user_can('view_all_trainer_data') && !current_user_can('manage_options')) {
			return '<p>You do not have permission to access Google Sheets integration.</p>';
		}
		
		// Initialize Google Sheets admin if not already done
		if (!class_exists('HVAC_Google_Sheets_Admin')) {
			require_once HVAC_PLUGIN_DIR . 'includes/google-sheets/class-google-sheets-admin.php';
		}
		
		$google_sheets_admin = new HVAC_Google_Sheets_Admin();
		
		ob_start();
		$google_sheets_admin->render_admin_page();
		return ob_get_clean();
	}
	
	/**
	 * Initialize Google Sheets integration
	 */
	private function init_google_sheets() {
		// Always initialize auth handler for OAuth callbacks
		if (file_exists(HVAC_PLUGIN_DIR . 'includes/google-sheets/class-google-sheets-auth.php')) {
			require_once HVAC_PLUGIN_DIR . 'includes/google-sheets/class-google-sheets-auth.php';
			new HVAC_Google_Sheets_Auth();
		}
		
		// Initialize admin interface
		if (file_exists(HVAC_PLUGIN_DIR . 'includes/google-sheets/class-google-sheets-admin.php')) {
			require_once HVAC_PLUGIN_DIR . 'includes/google-sheets/class-google-sheets-admin.php';
			new HVAC_Google_Sheets_Admin();
		}
	}
	
	/**
	 * Initialize communication system
	 */
	private function init_communication_system() {
		// Check and install/update communication database tables
		if (class_exists('HVAC_Communication_Installer')) {
			HVAC_Communication_Installer::maybe_update();
		}
		
		// Initialize the communication scheduler (singleton)
		if (class_exists('HVAC_Communication_Scheduler')) {
			hvac_communication_scheduler();
		}
		
		if (class_exists('HVAC_Logger')) {
			HVAC_Logger::info('Communication system initialized', 'Communication System');
		}
	}
	
	/**
	 * Render communication templates content
	 */
	public function render_communication_templates() {
		if (!is_user_logged_in()) {
			return '<p>Please log in to manage communication templates.</p>';
		}
		
		// Check if user is a trainer or has permission to manage templates
		$current_user = wp_get_current_user();
		if (!in_array('hvac_trainer', $current_user->roles) && !current_user_can('edit_posts')) {
			return '<p>You do not have permission to manage communication templates.</p>';
		}
		
		// Initialize the communication templates class
		if (!class_exists('HVAC_Communication_Templates')) {
			require_once HVAC_PLUGIN_DIR . 'includes/communication/class-communication-templates.php';
		}
		
		ob_start();
		include HVAC_PLUGIN_DIR . 'templates/communication/template-communication-templates.php';
		return ob_get_clean();
	}
	
	/**
	 * Render communication schedules content
	 */
	public function render_communication_schedules() {
		if (!is_user_logged_in()) {
			return '<p>Please log in to manage communication schedules.</p>';
		}
		
		// Check if user is a trainer or has permission to manage schedules
		$current_user = wp_get_current_user();
		if (!in_array('hvac_trainer', $current_user->roles) && !current_user_can('edit_posts')) {
			return '<p>You do not have permission to manage communication schedules.</p>';
		}
		
		// Initialize the communication scheduler class
		if (!class_exists('HVAC_Communication_Scheduler')) {
			require_once HVAC_PLUGIN_DIR . 'includes/communication/class-communication-scheduler.php';
		}
		
		ob_start();
		include HVAC_PLUGIN_DIR . 'templates/communication/template-communication-schedules.php';
		return ob_get_clean();
	}
	
	/**
	 * Enqueue scripts and styles for HVAC pages
	 */
	public function enqueue_scripts() {
		// Check if we're on any HVAC page
		$current_path = trim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/');
		$is_hvac_page = false;
		
		// List of HVAC page patterns
		$hvac_patterns = array(
			'trainer/',
			'master-trainer/',
			'training-login',
			'registration-pending',
			'trainer-account-'
		);
		
		// Check if current path matches any HVAC pattern
		foreach ($hvac_patterns as $pattern) {
			if (strpos($current_path, $pattern) === 0 || $current_path === rtrim($pattern, '/')) {
				$is_hvac_page = true;
				break;
			}
		}
		
		// Also check if any HVAC shortcodes are present
		global $post;
		if (is_a($post, 'WP_Post')) {
			$hvac_shortcodes = array(
				'hvac_dashboard',
				'hvac_master_dashboard',
				'hvac_event_summary',
				'hvac_trainer_profile',
				'hvac_email_attendees',
				'hvac_certificate_reports',
				'hvac_generate_certificates',
				'hvac_google_sheets',
				'hvac_communication_templates',
				'hvac_communication_schedules'
			);
			
			foreach ($hvac_shortcodes as $shortcode) {
				if (has_shortcode($post->post_content, $shortcode)) {
					$is_hvac_page = true;
					break;
				}
			}
		}
		
		if (!$is_hvac_page) {
			return;
		}
		
		// Enqueue common HVAC styles
		wp_enqueue_style(
			'hvac-common-style',
			HVAC_PLUGIN_URL . 'assets/css/hvac-common.css',
			array(), 
			HVAC_PLUGIN_VERSION
		);
		
		// Enqueue harmonized framework
		wp_enqueue_style(
			'hvac-harmonized-framework',
			HVAC_PLUGIN_URL . 'assets/css/hvac-harmonized.css',
			array('hvac-common-style'), 
			HVAC_PLUGIN_VERSION
		);
		
		// Enqueue main plugin CSS
		wp_enqueue_style(
			'hvac-community-events',
			HVAC_PLUGIN_URL . 'assets/css/hvac-community-events.css',
			array('hvac-harmonized-framework'),
			HVAC_PLUGIN_VERSION
		);
		
		// Enqueue main plugin JavaScript
		wp_enqueue_script(
			'hvac-community-events',
			HVAC_PLUGIN_URL . 'assets/js/hvac-community-events.js',
			array('jquery'),
			HVAC_PLUGIN_VERSION,
			true
		);
		
		// Localize script
		wp_localize_script('hvac-community-events', 'hvac_ajax', array(
			'ajax_url' => admin_url('admin-ajax.php'),
			'nonce' => wp_create_nonce('hvac_ajax_nonce')
		));
	}

	/**
	 * Clear master dashboard cache when posts are saved/deleted
	 *
	 * @param int $post_id Post ID
	 */
	public function clear_master_dashboard_cache($post_id) {
		$post_type = get_post_type($post_id);
		
		// Clear cache for event-related post types or ticket types
		$cache_trigger_types = [
			'tribe_events',
			'tec_tc_attendee', 
			'tribe_tpp_attendees',
			'tribe_rsvp_attendees'
		];
		
		if (in_array($post_type, $cache_trigger_types)) {
			if (class_exists('HVAC_Master_Dashboard_Data')) {
				HVAC_Master_Dashboard_Data::clear_cache();
			}
		}
	}

	/**
	 * Clear master dashboard cache when user data changes
	 */
	public function clear_master_dashboard_cache_on_user_change() {
		if (class_exists('HVAC_Master_Dashboard_Data')) {
			HVAC_Master_Dashboard_Data::clear_cache();
		}
	}

	/**
	 * Process TEC Community Events excerpt field
	 * 
	 * Handles the custom excerpt field added via template override.
	 * This method is called when TEC Community Events saves an event.
	 *
	 * @param int $event_id The event ID being saved
	 * @since 1.0.0 - HVAC Template Override Support
	 */
	public function process_tec_excerpt_field($event_id) {
		// Verify we have a valid event ID
		if (!$event_id || !is_numeric($event_id)) {
			return;
		}

		// Verify this is actually an event post
		$event = get_post($event_id);
		if (!$event || $event->post_type !== 'tribe_events') {
			return;
		}

		// Verify nonce (TEC handles this, but double-check for security)
		if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'ecp_event_submission')) {
			HVAC_Logger::warning("TEC excerpt processing: Nonce verification failed for event {$event_id}", 'TEC Template Override');
			return;
		}

		// Process excerpt field if present
		if (isset($_POST['post_excerpt'])) {
			$excerpt = sanitize_textarea_field($_POST['post_excerpt']);
			
			// Update the post excerpt
			$result = wp_update_post(array(
				'ID' => $event_id,
				'post_excerpt' => $excerpt
			), true);

			if (is_wp_error($result)) {
				HVAC_Logger::error("Failed to update excerpt for event {$event_id}: " . $result->get_error_message(), 'TEC Template Override');
			} else {
				HVAC_Logger::info("Excerpt field processed successfully for event {$event_id}: " . substr($excerpt, 0, 50) . (strlen($excerpt) > 50 ? '...' : ''), 'TEC Template Override');
			}
		}
	}

	/**
	 * Initialize TEC Field Processor System (Phase 2)
	 * 
	 * Sets up the modular field processing architecture with individual
	 * processors for each field type.
	 */
	private function init_tec_field_processor() {
		// Only initialize if classes are available
		if (!class_exists('HVAC_TEC_Field_Processor') || 
		    !class_exists('HVAC_TEC_Categories_Processor') ||
		    !class_exists('HVAC_TEC_FeaturedImage_Processor')) {
			HVAC_Logger::warning('TEC field processor classes not available - using legacy excerpt processing', 'TEC Template Override');
			// Fall back to legacy method
			add_action('tribe_events_community_before_event_save', array($this, 'process_tec_excerpt_field'));
			return;
		}

		// Create the main field processor
		$this->tec_field_processor = new HVAC_TEC_Field_Processor();

		// Register field processors
		$this->register_tec_field_processors();

		HVAC_Logger::info('TEC field processor system initialized with modular architecture', 'TEC Template Override');
	}

	/**
	 * Register individual TEC field processors
	 */
	private function register_tec_field_processors() {
		if (!$this->tec_field_processor) {
			return;
		}

		// Register categories processor
		$categories_processor = new HVAC_TEC_Categories_Processor();
		$result = $this->tec_field_processor->register_processor('categories', $categories_processor);
		if (is_wp_error($result)) {
			HVAC_Logger::error('Failed to register categories processor: ' . $result->get_error_message(), 'TEC Template Override');
		}

		// Register featured image processor
		$featured_image_processor = new HVAC_TEC_FeaturedImage_Processor();
		$result = $this->tec_field_processor->register_processor('featured_image', $featured_image_processor);
		if (is_wp_error($result)) {
			HVAC_Logger::error('Failed to register featured image processor: ' . $result->get_error_message(), 'TEC Template Override');
		}

		// Register excerpt processor (converted to new system)
		$excerpt_processor = new HVAC_TEC_Excerpt_Processor();
		$result = $this->tec_field_processor->register_processor('excerpt', $excerpt_processor);
		if (is_wp_error($result)) {
			HVAC_Logger::error('Failed to register excerpt processor: ' . $result->get_error_message(), 'TEC Template Override');
		}

		// Allow other plugins/code to register additional processors
		do_action('hvac_tec_register_field_processors', $this->tec_field_processor);

		$registered_count = count($this->tec_field_processor->get_registered_processors());
		HVAC_Logger::info("Registered {$registered_count} TEC field processors", 'TEC Template Override');
	}

	/**
	 * Process all TEC fields using the modular processor system (Phase 2)
	 * 
	 * This replaces the legacy process_tec_excerpt_field method with a comprehensive
	 * field processing system that can handle multiple field types with proper
	 * validation, security, and rollback capabilities.
	 * 
	 * @param int $event_id Event ID being processed
	 */
	public function process_all_tec_fields($event_id) {
		// Fall back to legacy method if processor not initialized
		if (!$this->tec_field_processor) {
			HVAC_Logger::warning('TEC field processor not initialized - falling back to legacy excerpt processing', 'TEC Template Override');
			$this->process_tec_excerpt_field($event_id);
			return;
		}

		// Process all registered fields
		$result = $this->tec_field_processor->process_all_fields($event_id, $_POST);

		if (is_wp_error($result)) {
			// Log the error
			HVAC_Logger::error("TEC field processing failed for event {$event_id}: " . $result->get_error_message(), 'TEC Template Override');

			// Add user-facing error message for TEC Community Events
			add_filter('tribe_events_community_submission_errors', function($errors) use ($result) {
				$errors[] = 'Event field processing failed: ' . $result->get_error_message();
				return $errors;
			});

			// Fall back to legacy excerpt processing as last resort
			HVAC_Logger::info("Falling back to legacy excerpt processing for event {$event_id}", 'TEC Template Override');
			$this->process_tec_excerpt_field($event_id);
		} else {
			// Log successful processing
			$metrics = $this->tec_field_processor->get_performance_metrics();
			HVAC_Logger::info(
				sprintf('TEC field processing completed successfully for event %d: %d fields processed in %.3fs',
					$event_id, $metrics['processed_fields_count'], $metrics['processing_time']),
				'TEC Template Override',
				$metrics
			);
		}
	}

	/**
	 * Get TEC field processor instance
	 * 
	 * @return HVAC_TEC_Field_Processor|null Field processor instance or null if not initialized
	 */
	public function get_tec_field_processor() {
		return $this->tec_field_processor;
	}

} // End class HVAC_Community_Events