<?php
/**
 * HVAC Page Content Manager
 * 
 * Ensures all plugin pages have the correct shortcodes in their content
 *
 * @package HVAC_Community_Events
 * @since 2.1.0
 */

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

class HVAC_Page_Content_Manager {
    
    /**
     * Instance
     */
    private static $instance = null;
    
    /**
     * Page shortcode mappings
     */
    private $page_shortcodes = array(
        // Trainer pages
        'trainer/venue/list' => '[hvac_trainer_venues_list]',
        'trainer/venue/manage' => '[hvac_trainer_venue_manage]',
        'trainer/organizer/manage' => '[hvac_trainer_organizer_manage]',
        'trainer/profile/training-leads' => '[hvac_trainer_training_leads]',
        
        // Master trainer pages
        'master-trainer/announcements' => '[hvac_announcements_timeline]',
        'master-trainer/trainers' => '[hvac_master_trainers]',
        'master-trainer/pending-approvals' => '[hvac_pending_approvals]',
        'master-trainer/events' => '[hvac_master_events]',
        'master-trainer/google-sheets' => '[hvac_google_sheets]',
    );
    
    /**
     * Get instance
     */
    public static function instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }
    
    /**
     * Constructor
     */
    private function __construct() {
        // Hook into page content filter
        add_filter('the_content', array($this, 'ensure_page_content'), 5);
        
        // Hook into plugin activation to update pages
        add_action('hvac_plugin_activated', array($this, 'update_all_page_content'));
        
        // Admin action to fix pages
        add_action('admin_init', array($this, 'maybe_fix_pages'));
    }
    
    /**
     * Ensure page has correct content
     */
    public function ensure_page_content($content) {
        if (!is_page()) {
            return $content;
        }
        
        global $post;
        if (!$post) {
            return $content;
        }
        
        // Get the page path
        $page_path = $this->get_page_path($post);
        
        // Check if this is one of our pages
        if (!isset($this->page_shortcodes[$page_path])) {
            return $content;
        }
        
        $expected_shortcode = $this->page_shortcodes[$page_path];
        
        // If content is empty or doesn't contain the shortcode, add it
        if (empty(trim($content)) || strpos($content, $expected_shortcode) === false) {
            // Return the shortcode to be processed
            return $expected_shortcode;
        }
        
        return $content;
    }
    
    /**
     * Get page path from post
     */
    private function get_page_path($post) {
        $path = '';
        
        // Build path from slug and parent
        if ($post->post_parent) {
            $parent = get_post($post->post_parent);
            if ($parent) {
                $path = $this->get_page_path($parent) . '/';
            }
        }
        
        $path .= $post->post_name;
        
        return trim($path, '/');
    }
    
    /**
     * Update all page content
     */
    public function update_all_page_content() {
        foreach ($this->page_shortcodes as $path => $shortcode) {
            $this->update_page_by_path($path, $shortcode);
        }
    }
    
    /**
     * Update a specific page by path
     */
    private function update_page_by_path($path, $shortcode) {
        // Find the page by path
        $page = get_page_by_path($path);
        
        if (!$page) {
            return;
        }
        
        // Check if page content needs updating
        if (empty($page->post_content) || strpos($page->post_content, $shortcode) === false) {
            wp_update_post(array(
                'ID' => $page->ID,
                'post_content' => $shortcode
            ));
        }
    }
    
    /**
     * Maybe fix pages on admin init
     */
    public function maybe_fix_pages() {
        // Only run once per day
        $last_check = get_option('hvac_pages_last_check', 0);
        if (time() - $last_check < DAY_IN_SECONDS) {
            return;
        }
        
        // Check if we're on an admin page
        if (!is_admin()) {
            return;
        }
        
        // Update the check time
        update_option('hvac_pages_last_check', time());
        
        // Fix all pages
        $this->update_all_page_content();
    }
    
    /**
     * Force fix all pages (can be called manually)
     */
    public function force_fix_all_pages() {
        foreach ($this->page_shortcodes as $path => $shortcode) {
            $page = get_page_by_path($path);
            
            if ($page) {
                wp_update_post(array(
                    'ID' => $page->ID,
                    'post_content' => $shortcode
                ));
                
                error_log("HVAC: Updated page {$path} with shortcode {$shortcode}");
            } else {
                error_log("HVAC: Could not find page {$path}");
            }
        }
        
        // Clear any caches
        if (function_exists('wp_cache_flush')) {
            wp_cache_flush();
        }
    }
}

// Initialize
add_action('init', function() {
    if (class_exists('HVAC_Page_Content_Manager')) {
        HVAC_Page_Content_Manager::instance();
    }
});