<?php

namespace WPUltimateSecurity\UltimateSecurity\Presentation\Admin;

/**
 * React Assets Manager
 *
 * Handles enqueuing of React-based 2FA profile settings
 */
class ReactAssets
{
    /**
     * Register hooks
     */
    public static function register()
    {
        add_action('admin_enqueue_scripts', [self::class, 'enqueue_profile_2fa_scripts']);
    }

    /**
     * Enqueue React scripts for 2FA profile page
     *
     * @param string $hook Current admin page hook
     */
    public static function enqueue_profile_2fa_scripts($hook)
    {
        // Only load on user profile pages
        if ($hook !== 'profile.php' && $hook !== 'user-edit.php') {
            return;
        }

        // Check if React build file exists
        $react_bundle_path = ULTIMATE_SECURITY_PATH . 'build/user-profile-2fa.js';
        $react_bundle_url = ULTIMATE_SECURITY_URL . 'build/user-profile-2fa.js';
        $asset_file = ULTIMATE_SECURITY_PATH . 'build/user-profile-2fa.asset.php';

        if (!file_exists($react_bundle_path)) {
            // Fall back to traditional template if React build doesn't exist
            error_log('Ultimate Security: React 2FA bundle not found. Run "npm run build" to create it.');
            return;
        }

        // Load asset file for dependencies and version
        $asset = file_exists($asset_file) ? require($asset_file) : ['dependencies' => [], 'version' => ULTIMATE_SECURITY_VERSION];

        // Enqueue React bundle with proper dependencies
        wp_enqueue_script(
            'ultimate-security-profile-2fa-react',
            $react_bundle_url,
            $asset['dependencies'],
            $asset['version'],
            true
        );
    }

    /**
     * Check if React mode is enabled
     *
     * @return bool
     */
    public static function is_react_enabled()
    {
        // Check if React build exists
        $react_bundle_path = ULTIMATE_SECURITY_PATH . 'build/user-profile-2fa.js';
        return file_exists($react_bundle_path);
    }
}
