<?php
/**
 * PRODUCTS MANAGEMENT - PERMISSION ENFORCEMENT MODULE
 * 
 * This module enforces the permission hierarchy for product management.
 * Include this at the top of your product admin pages.
 * 
 * Usage in admin_products.php (top, after session_start):
 *   require_once __DIR__ . '/helpers/enforce_manage_products.php';
 *   EnforceManageProducts::guardPage();  // Ensure user has view access
 *   $caps = EnforceManageProducts::capabilities();
 * 
 * Usage in HTML (conditional rendering):
 *   <?php if ($caps['can_delete']): ?>
 *       <button onclick="deleteProduct(...)">Delete</button>
 *   <?php endif; ?>
 * 
 * Usage in API (before processing requests):
 *   if ($_POST['action'] === 'delete') {
 *       EnforceManageProducts::guardAction('delete');
 *       // Process delete...
 *   }
 */

require_once __DIR__ . '/admin_page_header.php';

class EnforceManageProducts {
    
    const FEATURE = 'manage_products';
    
    /**
     * Guard page access - ensures user can view products
     * Exits with redirect if not authorized
     */
    public static function guardPage() {
        AdminPageHeader::requirePage(self::FEATURE);
    }
    
    /**
     * Guard specific action (create, edit, delete)
     * Exits with 403 JSON if not authorized  
     * @param string $action Action to guard
     */
    public static function guardAction($action) {
        AdminPageHeader::requireAction(self::FEATURE, $action);
    }
    
    /**
     * Get user's capabilities for display logic
     * @return array ['can_view', 'can_create', 'can_edit', 'can_delete', 'level']
     */
    public static function capabilities() {
        $guard = AdminPageHeader::getGuard(self::FEATURE);
        return $guard->getCapabilities();
    }
    
    /**
     * Quick permission check
     * @param string $action Action to check
     * @return bool
     */
    public static function can($action) {
        return AdminPageHeader::can(self::FEATURE, $action);
    }
    
    /**
     * Get user's permission level for products
     * @return string 'administrator', 'editor', 'viewer', 'none'
     */
    public static function level() {
        return AdminPageHeader::getLevel(self::FEATURE);
    }
}

?>
