<?php
/**
 * PERMISSION ENFORCEMENT INITIALIZATION
 * 
 * This file loads and initializes all enforcement modules.
 * It provides a unified interface for permission checking across the system.
 * 
 * Include this at the top of any admin page:
 *   require_once __DIR__ . '/helpers/enforce_init.php';
 *   EnforceInit::checkFeature('manage_products');  // Will check page access
 */

// Load all enforcement modules
require_once __DIR__ . '/admin_page_header.php';
require_once __DIR__ . '/subadmin_page_guard.php';
require_once __DIR__ . '/subadmin_actions.php';

// Load feature-specific enforcement modules 
require_once __DIR__ . '/enforce_manage_products.php';
require_once __DIR__ . '/enforce_manage_blogs.php';
require_once __DIR__ . '/enforce_manage_news.php';
require_once __DIR__ . '/enforce_manage_users.php';
require_once __DIR__ . '/enforce_manage_orders.php';
require_once __DIR__ . '/enforce_manage_pages.php';
require_once __DIR__ . '/enforce_manage_carousel.php';
require_once __DIR__ . '/enforce_manage_video.php';
require_once __DIR__ . '/enforce_manage_slider_images.php';
require_once __DIR__ . '/enforce_manage_slider_settings.php';
require_once __DIR__ . '/enforce_manage_site_title.php';
require_once __DIR__ . '/enforce_manage_logo.php';
require_once __DIR__ . '/enforce_manage_colors.php';
require_once __DIR__ . '/enforce_manage_category_carousel.php';
require_once __DIR__ . '/enforce_manage_social.php';
require_once __DIR__ . '/enforce_manage_seo.php';
require_once __DIR__ . '/enforce_manage_footer_settings.php';
require_once __DIR__ . '/enforce_manage_homepage.php';
require_once __DIR__ . '/enforce_manage_marquee.php';
require_once __DIR__ . '/enforce_manage_footer_marquee.php';
require_once __DIR__ . '/enforce_manage_featured_categories.php';
require_once __DIR__ . '/enforce_manage_latest_products.php';
require_once __DIR__ . '/enforce_manage_awards.php';
require_once __DIR__ . '/enforce_manage_subadmins.php';
require_once __DIR__ . '/enforce_manage_settings.php';

class EnforceInit {
    
    /**
     * Map of feature names to enforcement class names
     */
    private static $classList = [
        'manage_products' => 'EnforceManageProducts',
        'manage_blogs' => 'EnforceManageBlogs',
        'manage_news' => 'EnforceManageNews',
        'manage_users' => 'EnforceManageUsers',
        'manage_orders' => 'EnforceManageOrders',
        'manage_pages' => 'EnforceManagePages',
        'manage_carousel' => 'EnforceManageCarousel',
        'manage_video' => 'EnforceManageVideo',
        'manage_slider_images' => 'EnforceManageSliderImages',
        'manage_slider_settings' => 'EnforceManageSliderSettings',
        'manage_site_title' => 'EnforceManageSiteTitle',
        'manage_logo' => 'EnforceManageLogo',
        'manage_colors' => 'EnforceManageColors',
        'manage_categories' => 'EnforceManageCategoryCarousel',
        'manage_social' => 'EnforceManageSocial',
        'manage_seo' => 'EnforceManageSeo',
        'manage_footer_settings' => 'EnforceManageFooterSettings',
        'manage_homepage' => 'EnforceManageHomepage',
        'manage_marquee' => 'EnforceManageMarquee',
        'manage_footer_marquee' => 'EnforceManageFooterMarquee',
        'manage_featured_categories' => 'EnforceManageFeaturedCategories',
        'manage_latest_products' => 'EnforceManageLatestProducts',
        'manage_awards' => 'EnforceManageAwards',
        'manage_subadmins' => 'EnforceManageSubadmins',
        'manage_settings' => 'EnforceManageSettings',
    ];
    
    /**
     * Quick feature access check and enforcement
     * Usage: EnforceInit::checkFeature('manage_products');
     * 
     * @param string $feature Feature name
     * @return bool True if authorized
     */
    public static function checkFeature($feature) {
        $className = self::$classList[$feature] ?? null;
        if ($className && class_exists($className)) {
            return call_user_func([$className, 'guardPage']);
        }
        // Fallback to generic check
        return AdminPageHeader::requirePage($feature);
    }
    
    /**
     * Quick action enforcement
     * Usage: EnforceInit::checkAction('manage_products', 'delete');
     * 
     * @param string $feature Feature name
     * @param string $action Action name
     */
    public static function checkAction($feature, $action) {
        $className = self::$classList[$feature] ?? null;
        if ($className && class_exists($className)) {
            return call_user_func([$className, 'guardAction'], $action);
        }
        // Fallback to generic check
        return AdminPageHeader::requireAction($feature, $action);
    }
    
    /**
     * Get capabilities for a feature
     * Usage: $caps = EnforceInit::getCapabilities('manage_products');
     * 
     * @param string $feature Feature name
     * @return array
     */
    public static function getCapabilities($feature) {
        $className = self::$classList[$feature] ?? null;
        if ($className && class_exists($className)) {
            return call_user_func([$className, 'capabilities']);
        }
        // Fallback
        return AdminPageHeader::getGuard($feature)->getCapabilities();
    }
    
    /**
     * Quick permission check
     * Usage: if (EnforceInit::can('manage_products', 'delete')) { ... }
     * 
     * @param string $feature Feature name
     * @param string $action Action name
     * @return bool
     */
    public static function can($feature, $action) {
        $className = self::$classList[$feature] ?? null;
        if ($className && class_exists($className)) {
            return call_user_func([$className, 'can'], $action);
        }
        // Fallback
        return AdminPageHeader::can($feature, $action);
    }
    
    /**
     * Get permission level
     * Usage: $level = EnforceInit::getLevel('manage_products');
     * Returns: 'administrator', 'editor', 'viewer', 'none'
     * 
     * @param string $feature Feature name
     * @return string
     */
    public static function getLevel($feature) {
        $className = self::$classList[$feature] ?? null;
        if ($className && class_exists($className)) {
            return call_user_func([$className, 'level']);
        }
        // Fallback
        return AdminPageHeader::getLevel($feature);
    }
    
    /**
     * Get status of enforcement system
     * @return array Status information
     */
    public static function getStatus() {
        return [
            'status' => 'ACTIVE',
            'version' => '1.0',
            'enforced_features' => count(self::$classList),
            'features' => array_keys(self::$classList),
            'helpers_loaded' => [
                'admin_page_header' => true,
                'subadmin_page_guard' => true,
                'subadmin_actions' => true,
            ]
        ];
    }
}

// Auto-initialization: log that enforcement is loaded
if (!defined('ENFORCE_INIT_LOADED')) {
    define('ENFORCE_INIT_LOADED', true);
}

?>
