<?php
/**
 * Plugin Name: HVAC Community Events
 * Plugin URI: https://upskillhvac.com
 * Description: Custom plugin for HVAC trainer event management system
 * Version: 1.0.0
 * Author: Upskill HVAC
 * Author URI: https://upskillhvac.com
 * License: GPL-2.0+
 * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
 * Text Domain: hvac-community-events
 */

// Exit if accessed directly
if (!defined('ABSPATH')) {
    exit;
}

// Define plugin constants
define('HVAC_CE_VERSION', '1.0.0');
define('HVAC_CE_PLUGIN_FILE', __FILE__);
define('HVAC_CE_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('HVAC_CE_PLUGIN_URL', plugin_dir_url(__FILE__));

// Include the logger class early
require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-logger.php';


/**
 * Create required pages and roles upon plugin activation.
 */
function hvac_ce_create_required_pages() {

    // Ensure the roles class is available
    require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-roles.php';
    HVAC_Logger::info('Starting page creation process', 'Activation');
    
    // Initialize certificate security early to register rewrite rules before flush
    if (file_exists(HVAC_CE_PLUGIN_DIR . 'includes/certificates/class-certificate-security.php')) {
        require_once HVAC_CE_PLUGIN_DIR . 'includes/certificates/class-certificate-security.php';
        $cert_security = HVAC_Certificate_Security::instance();
        $cert_security->init_secure_download();
        HVAC_Logger::info('Certificate security initialized during activation', 'Activation');
    }
    $required_pages = [
        'community-login' => [
            'title' => 'Trainer Login',
            'content' => '<!-- wp:shortcode -->[hvac_community_login]<!-- /wp:shortcode -->',
            'template' => 'page-community-login.php',
        ],
        'trainer-registration' => [
            'title' => 'Trainer Registration',
            'content' => '<!-- wp:shortcode -->[hvac_trainer_registration]<!-- /wp:shortcode -->',
        ],
        'hvac-dashboard' => [
            'title' => 'Trainer Dashboard',
            'content' => '<!-- wp:shortcode -->[hvac_trainer_dashboard]<!-- /wp:shortcode -->',
        ],
        'manage-event' => [ // New page for TEC CE submission form shortcode
            'title' => 'Manage Event',
            'content' => '<!-- wp:shortcode -->[tribe_community_events view="submission_form"]<!-- /wp:shortcode -->',
        ],
        'trainer-profile' => [ // Add trainer profile page
            'title' => 'Trainer Profile',
            'content' => '<!-- wp:shortcode -->[hvac_trainer_profile]<!-- /wp:shortcode -->',
        ],
        'event-summary' => [ // Add event summary page
            'title' => 'Event Summary',
            'content' => '<!-- wp:shortcode -->[hvac_event_summary]<!-- /wp:shortcode -->',
        ],
        'email-attendees' => [ // Add email attendees page
            'title' => 'Email Attendees',
            'content' => '<!-- wp:shortcode -->[hvac_email_attendees]<!-- /wp:shortcode -->',
        ],
        'certificate-reports' => [ // Add certificate reports page
            'title' => 'Certificate Reports',
            'content' => '<!-- wp:shortcode -->[hvac_certificate_reports]<!-- /wp:shortcode -->',
        ],
        'generate-certificates' => [ // Add generate certificates page
            'title' => 'Generate Certificates',
            'content' => '<!-- wp:shortcode -->[hvac_generate_certificates]<!-- /wp:shortcode -->',
        ],
        'certificate-fix' => [ // Add certificate fix page (admin only)
            'title' => 'Certificate System Diagnostics',
            'content' => '<!-- wp:shortcode -->[hvac_certificate_fix]<!-- /wp:shortcode -->',
        ],
        'hvac-documentation' => [ // Add documentation page
            'title' => 'Trainer Documentation',
            'content' => '<!-- wp:shortcode -->[hvac_documentation]<!-- /wp:shortcode -->',
        ],
        'attendee-profile' => [ // Add attendee profile page
            'title' => 'Attendee Profile',
            'content' => '<!-- wp:shortcode -->[hvac_attendee_profile]<!-- /wp:shortcode -->',
        ],
        'master-dashboard' => [ // Add master dashboard page
            'title' => 'Master Dashboard',
            'content' => '<!-- wp:shortcode -->[hvac_master_dashboard]<!-- /wp:shortcode -->',
        ],
        'google-sheets' => [ // Add Google Sheets admin page
            'title' => 'Google Sheets Integration',
            'content' => '<!-- wp:shortcode -->[hvac_google_sheets]<!-- /wp:shortcode -->',
        ],
        'communication-templates' => [ // Add Communication Templates page
            'title' => 'Communication Templates',
            'content' => '<!-- wp:shortcode -->[hvac_communication_templates]<!-- /wp:shortcode -->',
        ],
        'communication-schedules' => [ // Add Communication Schedules page
            'title' => 'Communication Schedules',
            'content' => '<!-- wp:shortcode -->[hvac_communication_schedules]<!-- /wp:shortcode -->',
        ],
        // REMOVED: 'submit-event' page creation. Will link to default TEC CE page.
        // 'submit-event' => [
        //     'title' => 'Submit Event',
        //     'content' => '<!-- wp:shortcode -->[hvac_event_form]<!-- /wp:shortcode -->',
        // ],
        // Add future required pages here
    ];

    $created_pages_option = 'hvac_community_pages';
    $created_pages = get_option($created_pages_option, []);

    foreach ($required_pages as $slug => $page_data) {
        // Check if page already exists (by slug)
        $existing_page = get_page_by_path($slug, OBJECT, 'page');
        
        // Log what we're getting back for debugging
        HVAC_Logger::info("Checking for page with slug '{$slug}'. Result type: " . gettype($existing_page), 'Activation');
        
        if (!$existing_page) {
            HVAC_Logger::info("Page with slug '{$slug}' not found. Attempting to create.", 'Activation');
            // Page does not exist, create it
            $post_data = [
                'post_title'   => $page_data['title'],
                'post_name'    => $slug,
                'post_content' => $page_data['content'],
                'post_status'  => 'publish',
                'post_type'    => 'page',
                'comment_status' => 'closed',
                'ping_status'  => 'closed',
            ];
            
            // Check if we should use a specific template
            if (!empty($page_data['template'])) {
                $post_data['page_template'] = $page_data['template'];
            }

            $page_id = wp_insert_post($post_data);

            // Log the result of wp_insert_post
            if (is_wp_error($page_id)) {
                HVAC_Logger::error("Error creating page '{$slug}': " . $page_id->get_error_message(), 'Activation');
            } else {
                HVAC_Logger::info("Successfully created page '{$slug}' with ID: {$page_id}.", 'Activation');
            }

            // Store the created page ID - Rewritten to avoid tool issue with &&
            if ($page_id) { // Check if page_id is truthy (non-zero, non-null, etc.)
                if (!is_wp_error($page_id)) { // Then check if it's not a WP_Error object
                    // Use a key based on the slug or feature name for clarity
                    $feature_key = str_replace('-', '_', $slug);
                    $created_pages[$feature_key] = $page_id;
                }
            }
        } else {
             // Ensure existing pages are also recorded in the option if not already
             $feature_key = str_replace('-', '_', $slug);
             
             // Check if the existing page is an object and has an ID property
             if (!isset($created_pages[$feature_key])) {
                 if (is_object($existing_page) && isset($existing_page->ID)) {
                     $created_pages[$feature_key] = $existing_page->ID;
                     HVAC_Logger::info("Page '{$slug}' exists. Recording ID: {$existing_page->ID}", 'Activation');
                 } else {
                     // If existing_page is not valid, log it but don't cause an error
                     HVAC_Logger::warning("Page '{$slug}' exists but could not retrieve ID properly.", 'Activation');
                 }
             }
        }
    }

    // Update the option with any newly created page IDs (and existing ones)
    update_option($created_pages_option, $created_pages);

    // Create the custom roles (Moved inside the activation function)
    $roles_manager = new HVAC_Roles();
    
    // Create trainer role
    $result = $roles_manager->create_trainer_role();
    if ($result) {
        HVAC_Logger::info('Successfully created hvac_trainer role.', 'Activation');
    } else {
        HVAC_Logger::error('Failed to create hvac_trainer role.', 'Activation');
    }
    
    // Create master trainer role
    $master_result = $roles_manager->create_master_trainer_role();
    if ($master_result) {
        HVAC_Logger::info('Successfully created hvac_master_trainer role.', 'Activation');
    } else {
        HVAC_Logger::error('Failed to create hvac_master_trainer role.', 'Activation');
    }
    
    // Grant administrators access to dashboard to prevent redirect loops
    $admin_access = $roles_manager->grant_admin_dashboard_access();
    if ($admin_access) {
        HVAC_Logger::info('Successfully granted admin dashboard access.', 'Activation');
    } else {
        HVAC_Logger::error('Failed to grant admin dashboard access.', 'Activation');
    }
    
    // Initialize certificate security to register rewrite rules
    require_once HVAC_CE_PLUGIN_DIR . 'includes/certificates/class-certificate-security.php';
    if (class_exists('HVAC_Certificate_Security')) {
        $cert_security = HVAC_Certificate_Security::instance();
        // Manually call init_secure_download to ensure rewrite rules are added
        if (method_exists($cert_security, 'init_secure_download')) {
            $cert_security->init_secure_download();
        }
        HVAC_Logger::info('Initialized certificate security for rewrite rules', 'Activation');
    }
    
    // Flush rewrite rules to ensure certificate download URLs work
    flush_rewrite_rules();
    HVAC_Logger::info('Flushed rewrite rules for certificate downloads', 'Activation');
    
    HVAC_Logger::info('Completed page creation and role setup process', 'Activation');

} // <<-- Brace moved here
register_activation_hook(__FILE__, 'hvac_ce_create_required_pages');


/**
 * Remove custom roles upon plugin deactivation.
 */
function hvac_ce_remove_roles() {
    require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-roles.php';
    $roles_manager = new HVAC_Roles();
    $roles_manager->remove_trainer_role();
    $roles_manager->remove_master_trainer_role();
    $roles_manager->revoke_admin_dashboard_access();
    
    // Flush rewrite rules to clean up certificate download URLs
    flush_rewrite_rules();
    
    HVAC_Logger::info('Deactivation hook fired, removed hvac_trainer role and admin dashboard access, flushed rewrite rules.', 'Deactivation');
}
register_deactivation_hook(__FILE__, 'hvac_ce_remove_roles');



/**
 * Enqueue common styles and scripts for HVAC Community Events pages
 */
function hvac_ce_enqueue_common_assets() {
    // Early return if not on HVAC pages to prevent loading on home page
    if (is_front_page() || is_home()) {
        return;
    }
    
    // Check if we're on an HVAC plugin page
    $hvac_pages = [
        'hvac-dashboard', 'community-login', 'trainer-registration', 'trainer-profile',
        'manage-event', 'event-summary', 'email-attendees', 'certificate-reports',
        'generate-certificates', 'certificate-fix', 'hvac-documentation', 'attendee-profile',
        'master-dashboard', 'google-sheets', 'communication-templates', 'communication-schedules'
    ];
    
    // Only proceed if we're on an HVAC page
    if (!is_page($hvac_pages)) {
        return;
    }
    
    // Enqueue the harmonized framework first - this provides the base styling
    wp_enqueue_style(
        'hvac-harmonized-framework',
        HVAC_CE_PLUGIN_URL . 'assets/css/hvac-harmonized.css',
        [], // No dependencies - this is the foundation
        HVAC_CE_VERSION . '-v3.0.0'
    );
        
    // Enqueue the legacy common CSS file for backward compatibility
    wp_enqueue_style(
        'hvac-common-style',
        HVAC_CE_PLUGIN_URL . 'assets/css/hvac-common.css',
        ['hvac-harmonized-framework'], // Depends on harmonized framework
        HVAC_CE_VERSION
    );
        
    // Enqueue animations CSS file (ONLY on HVAC pages)
    wp_enqueue_style(
        'hvac-animations',
        HVAC_CE_PLUGIN_URL . 'assets/css/hvac-animations.css',
        ['hvac-harmonized-framework'], // Depends on harmonized framework
        HVAC_CE_VERSION
    );
        
    // Enqueue mobile navigation CSS file (ONLY on HVAC pages)
    wp_enqueue_style(
        'hvac-mobile-nav',
        HVAC_CE_PLUGIN_URL . 'assets/css/hvac-mobile-nav.css',
        ['hvac-harmonized-framework'], // Depends on harmonized framework
        HVAC_CE_VERSION
    );
        
    // Enqueue print stylesheet
    wp_enqueue_style(
        'hvac-print-style',
        HVAC_CE_PLUGIN_URL . 'assets/css/hvac-print.css',
        ['hvac-harmonized-framework'], // Depends on harmonized framework
        HVAC_CE_VERSION,
        'print' // Print media only
    );
        
    // Enqueue the accessibility helper JS (ONLY on HVAC pages)
    wp_enqueue_script(
        'hvac-accessibility-js',
        HVAC_CE_PLUGIN_URL . 'assets/js/hvac-accessibility.js',
        [], // No dependencies
        HVAC_CE_VERSION,
        true // Load in footer
    );
        
    // Enqueue animations JS (ONLY on HVAC pages)
    wp_enqueue_script(
        'hvac-animations-js',
        HVAC_CE_PLUGIN_URL . 'assets/js/hvac-animations.js',
        [], // No dependencies
        HVAC_CE_VERSION,
        true // Load in footer
    );
        
    // Enqueue mobile navigation JS (ONLY on HVAC pages)
    wp_enqueue_script(
        'hvac-mobile-nav-js',
        HVAC_CE_PLUGIN_URL . 'assets/js/hvac-mobile-nav.js',
        [], // No dependencies
        HVAC_CE_VERSION,
        true // Load in footer
    );
    
    // Enqueue page-specific enhanced styles based on current page
    if (is_page('hvac-dashboard')) {
        wp_enqueue_style(
            'hvac-dashboard-enhanced',
            HVAC_CE_PLUGIN_URL . 'assets/css/hvac-dashboard-enhanced.css',
            ['hvac-harmonized-framework'], // Depends on harmonized framework
            HVAC_CE_VERSION . '-v3.0.0'
        );
        // Keep legacy for compatibility
        wp_enqueue_style(
            'hvac-dashboard-style',
            HVAC_CE_PLUGIN_URL . 'assets/css/hvac-dashboard.css',
            ['hvac-dashboard-enhanced'], // Load after enhanced
            HVAC_CE_VERSION
        );
    }
    
    if (is_page('master-dashboard')) {
        // Master dashboard uses same styling as regular dashboard
        wp_enqueue_style(
            'hvac-dashboard-enhanced',
            HVAC_CE_PLUGIN_URL . 'assets/css/hvac-dashboard-enhanced.css',
            ['hvac-harmonized-framework'], // Depends on harmonized framework
            HVAC_CE_VERSION . '-v3.0.0'
        );
        wp_enqueue_style(
            'hvac-dashboard-style',
            HVAC_CE_PLUGIN_URL . 'assets/css/hvac-dashboard.css',
            ['hvac-dashboard-enhanced'], // Load after enhanced
            HVAC_CE_VERSION
        );
    }
    
    if (is_page('community-login')) {
        wp_enqueue_style(
            'hvac-community-login-enhanced',
            HVAC_CE_PLUGIN_URL . 'assets/css/community-login-enhanced.css',
            ['hvac-harmonized-framework'], // Depends on harmonized framework
            HVAC_CE_VERSION . '-v3.0.0'
        );
        // Keep legacy for compatibility
        wp_enqueue_style(
            'hvac-community-login-style',
            HVAC_CE_PLUGIN_URL . 'assets/css/community-login.css',
            ['hvac-community-login-enhanced'], // Load after enhanced
            HVAC_CE_VERSION
        );
    }
    
    if (is_page('trainer-registration')) {
        wp_enqueue_style(
            'hvac-registration-style',
            HVAC_CE_PLUGIN_URL . 'assets/css/hvac-registration.css',
            ['hvac-common-style'], // Depends on common styles
            HVAC_CE_VERSION
        );
    }
    
    if (is_page('email-attendees')) {
        wp_enqueue_style(
            'hvac-email-attendees-style',
            HVAC_CE_PLUGIN_URL . 'assets/css/hvac-email-attendees.css',
            ['hvac-harmonized-framework'], // Depends on harmonized framework
            HVAC_CE_VERSION
        );
    }
    
    if (is_singular(Tribe__Events__Main::POSTTYPE) || is_page('event-summary')) {
        wp_enqueue_style(
            'hvac-event-summary-style',
            HVAC_CE_PLUGIN_URL . 'assets/css/hvac-event-summary.css',
            ['hvac-common-style'], // Depends on common styles
            HVAC_CE_VERSION
        );
        
        // Enqueue event summary JS for certificate actions
        wp_enqueue_script(
            'hvac-event-summary-js',
            HVAC_CE_PLUGIN_URL . 'assets/js/hvac-event-summary.js',
            ['jquery'], // jQuery dependency
            HVAC_CE_VERSION,
            true // Load in footer
        );
        
        // Localize script with AJAX data
        wp_localize_script('hvac-event-summary-js', 'hvacEventSummary', [
            'ajaxUrl' => admin_url('admin-ajax.php'),
            'certificateNonce' => wp_create_nonce('hvac_certificate_actions')
        ]);
    }
    
    // Enqueue certificate-related styles
    if (is_page('certificate-reports') || is_page('generate-certificates') || is_page('certificate-fix')) {
        wp_enqueue_style(
            'hvac-certificates-enhanced',
            HVAC_CE_PLUGIN_URL . 'assets/css/hvac-certificates-enhanced.css',
            ['hvac-harmonized-framework'], // Depends on harmonized framework
            HVAC_CE_VERSION . '-v3.0.0'
        );
        // Keep legacy for compatibility
        wp_enqueue_style(
            'hvac-certificates-admin-style',
            HVAC_CE_PLUGIN_URL . 'assets/css/hvac-certificates-admin.css',
            ['hvac-certificates-enhanced'], // Load after enhanced
            HVAC_CE_VERSION
        );
        
        // Enqueue certificate JS
        wp_enqueue_script(
            'hvac-certificate-admin-js',
            HVAC_CE_PLUGIN_URL . 'assets/js/hvac-certificate-admin.js',
            ['jquery', 'wp-color-picker'], // jQuery dependency
            HVAC_CE_VERSION,
            true // Load in footer
        );
        
        // Add WordPress color picker if needed
        wp_enqueue_style('wp-color-picker');
        wp_enqueue_script('wp-color-picker');
        
        // Localize script with AJAX data
        wp_localize_script('hvac-certificate-admin-js', 'hvacCertificateData', [
            'ajaxUrl' => admin_url('admin-ajax.php'),
            'previewNonce' => wp_create_nonce('hvac_certificate_preview')
        ]);
    }
}
add_action('wp_enqueue_scripts', 'hvac_ce_enqueue_common_assets');

/**
 * Enqueue styles and scripts for admin dashboard
 */
function hvac_ce_enqueue_admin_assets($hook) {
    // Only load on our dashboard page
    if ($hook !== 'hvac-community-events_page_hvac-ce-dashboard') {
        return;
    }
    
    // Enqueue dashboard CSS
    wp_enqueue_style(
        'hvac-admin-dashboard-style',
        HVAC_CE_PLUGIN_URL . 'assets/css/admin-dashboard.css',
        array('wp-admin'),
        HVAC_CE_VERSION
    );
    
    // Enqueue dashboard JS
    wp_enqueue_script(
        'hvac-admin-dashboard-script',
        HVAC_CE_PLUGIN_URL . 'assets/js/admin-dashboard.js',
        array('jquery', 'wp-util'),
        HVAC_CE_VERSION,
        true
    );
    
    // Localize script with AJAX data
    wp_localize_script('hvac-admin-dashboard-script', 'hvac_admin_dashboard', array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'nonce' => wp_create_nonce('hvac_admin_nonce')
    ));
}



// Include the main plugin class
require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-community-events.php';

// Include the help system
require_once HVAC_CE_PLUGIN_DIR . 'includes/class-hvac-help-system.php';

// Initialize the plugin
function hvac_community_events_init() {
    HVAC_Logger::info('Initializing HVAC Community Events plugin', 'Core');
    return HVAC_Community_Events::instance();
}
add_action('plugins_loaded', 'hvac_community_events_init');

// Initialize certificate URL handler very early to catch URLs before 404
function hvac_init_certificate_url_handler() {
    // Load the certificate URL handler class if not already loaded
    if (!class_exists('HVAC_Certificate_URL_Handler')) {
        $handler_file = HVAC_CE_PLUGIN_DIR . 'includes/certificates/class-certificate-url-handler.php';
        if (file_exists($handler_file)) {
            require_once $handler_file;
        }
    }
    
    // Initialize the handler if class exists
    if (class_exists('HVAC_Certificate_URL_Handler')) {
        HVAC_Certificate_URL_Handler::instance();
    }
}
// Hook very early - before WordPress decides it's a 404
add_action('muplugins_loaded', 'hvac_init_certificate_url_handler', 1);
add_action('plugins_loaded', 'hvac_init_certificate_url_handler', 1);


/**
 * Include custom template for single event summary page.
 *
 * @param string $template The path of the template to include.
 * @return string The path of the template file.
 */
function hvac_ce_include_event_summary_template( $template ) {
    // Check if it's a single event post type view
    if ( is_singular( Tribe__Events__Main::POSTTYPE ) ) {
        // Check if the custom template exists in the plugin's template directory
        $custom_template = HVAC_CE_PLUGIN_DIR . 'templates/single-hvac-event-summary.php';
        if ( file_exists( $custom_template ) ) {
            // Return the path to the custom template
            return $custom_template;
        }
    }
    // Return the original template if not a single event or custom template doesn't exist
    return $template;
}

/**
 * Template routing for Order Summary Page.
 */
function hvac_ce_include_order_summary_template( $template ) {
    if ( is_page( 'order-summary' ) && isset( $_GET['order_id'] ) && absint( $_GET['order_id'] ) > 0 ) {
        $custom_template = HVAC_CE_PLUGIN_DIR . 'templates/single-hvac-order-summary.php';
        if ( file_exists( $custom_template ) ) {
            return $custom_template;
        }
    }
    return $template;
}
// Removed - template handling is now in the main class
// add_filter( 'template_include', 'hvac_ce_include_event_summary_template', 99 );

/**
 * Initialize attendee profile handler
 */
function hvac_init_attendee_profile() {
    // Load the attendee profile class if not already loaded
    if (!class_exists('HVAC_Attendee_Profile')) {
        $profile_file = HVAC_CE_PLUGIN_DIR . 'includes/class-attendee-profile.php';
        if (file_exists($profile_file)) {
            require_once $profile_file;
        }
    }
    
    // Initialize the handler if class exists
    if (class_exists('HVAC_Attendee_Profile')) {
        HVAC_Attendee_Profile::instance();
    }
}
// Initialize on plugins_loaded
add_action('plugins_loaded', 'hvac_init_attendee_profile', 10);

// Include attendee profile helper functions
require_once HVAC_CE_PLUGIN_DIR . 'includes/helpers/attendee-profile-link.php';
