<?php
/**
 * HVAC Template Router
 * 
 * Manages template routing and page configuration for the base template system
 * 
 * @package HVAC_Community_Events
 * @since 2.0.0
 */

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

class HVAC_Template_Router {
    
    /**
     * Page configurations mapping slugs to render settings
     * 
     * @var array
     */
    private static $page_configs = [
        // Simple shortcode-based pages
        'trainer/certificate-reports' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_certificate_reports]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ],
        'trainer/generate-certificates' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_generate_certificates]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ],
        'trainer/profile/edit' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_trainer_profile_edit]',
            'show_navigation' => true,
            'show_breadcrumbs' => false,
            'menu_type' => 'trainer'
        ],
        'trainer/venue/list' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_trainer_venues_list]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ],
        'trainer/venue/manage' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_trainer_venue_manage]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ],
        'trainer/organizer/list' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_trainer_organizers_list]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ],
        'trainer/organizer/manage' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_trainer_organizer_manage]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ],
        'trainer/profile/training-leads' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_trainer_training_leads]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ],
        'trainer/announcements' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_trainer_announcements]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ],
        'trainer/resources' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_trainer_resources]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ],
        'trainer/documentation' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_documentation]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ],
        
        // Master trainer pages
        'master-trainer/announcements' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_master_announcements]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'master_trainer'
        ],
        'master-trainer/manage-announcements' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_master_manage_announcements]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'master_trainer'
        ],
        
        // Email and communication pages
        'trainer/email-attendees' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_email_attendees]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ],
        'trainer/communication-templates' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_communication_templates]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ],
        'trainer/communication-schedules' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_communication_schedules]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ],
        
        // Event management pages
        'trainer/event/summary' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_event_summary]',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ],
        
        // Public pages (no navigation)
        'community-login' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_community_login]',
            'show_navigation' => false,
            'show_breadcrumbs' => false
        ],
        'trainer/registration' => [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_trainer_registration]',
            'show_navigation' => false,
            'show_breadcrumbs' => false
        ],
        'registration-pending' => [
            'render_method' => 'template_part',
            'content_source' => 'templates/content/registration-pending',
            'show_navigation' => false,
            'show_breadcrumbs' => false
        ]
    ];
    
    /**
     * Get page configuration for a given slug
     * 
     * @param string $page_slug
     * @return array
     */
    public static function get_page_config($page_slug) {
        // Normalize slug
        $page_slug = trim($page_slug, '/');
        
        // Check direct match first
        if (isset(self::$page_configs[$page_slug])) {
            return self::$page_configs[$page_slug];
        }
        
        // Check for pattern matches
        foreach (self::$page_configs as $pattern => $config) {
            if (self::slug_matches_pattern($page_slug, $pattern)) {
                return $config;
            }
        }
        
        // Default configuration for unknown pages
        return [
            'render_method' => 'shortcode',
            'content_source' => '[hvac_' . str_replace(['/', '-'], '_', $page_slug) . ']',
            'show_navigation' => true,
            'show_breadcrumbs' => true,
            'menu_type' => 'trainer'
        ];
    }
    
    /**
     * Check if a slug matches a pattern
     * 
     * @param string $slug
     * @param string $pattern
     * @return bool
     */
    private static function slug_matches_pattern($slug, $pattern) {
        // Convert pattern to regex (basic implementation)
        $regex = str_replace(['*', '/'], ['[^/]*', '\/'], $pattern);
        $regex = '/^' . $regex . '$/';
        
        return preg_match($regex, $slug);
    }
    
    /**
     * Register additional page configuration
     * 
     * @param string $slug
     * @param array $config
     */
    public static function register_page_config($slug, $config) {
        self::$page_configs[$slug] = $config;
    }
    
    /**
     * Get all registered page configurations
     * 
     * @return array
     */
    public static function get_all_configs() {
        return self::$page_configs;
    }
    
    /**
     * Check if a page can use the base template
     * 
     * @param string $page_slug
     * @return bool
     */
    public static function can_use_base_template($page_slug) {
        $config = self::get_page_config($page_slug);
        
        // Complex pages that need their own templates
        $complex_pages = [
            'trainer/dashboard',
            'master-trainer/master-dashboard',
            'trainer/profile',
            'trainer/account-pending',
            'trainer/account-disabled',
            'trainer/event/edit',
            'trainer/event/create'
        ];
        
        return !in_array($page_slug, $complex_pages);
    }
}