<?php
/**
 * VMPFence 2FA Shortcode Handler
 * 
 * Provides [vmpfence_2fa] shortcode for frontend 2FA management
 * 
 * @package VMPFence
 * @since 2.1.0
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

class VMPFence_2FA_Shortcode {
    
    /**
     * Initialize the shortcode
     */
    public static function init() {
        // Only register shortcode if enabled
        if (self::is_enabled()) {
            add_shortcode('vmpfence_2fa', array(__CLASS__, 'render_shortcode'));
        }
    }
    
    /**
     * Check if shortcode is enabled
     */
    private static function is_enabled() {
        return VMPFence_Config::get('vmpfence_2fa_shortcode_enabled', false);
    }
    
    /**
     * Render the shortcode
     */
    public static function render_shortcode($atts) {
        // Check if user is logged in
        if (!is_user_logged_in()) {
            return '<div class="vmpfence-2fa-shortcode">' . 
                   '<p>' . __('Please log in to manage your Two-Factor Authentication settings.', 'vmpfence-security') . '</p>' .
                   '</div>';
        }
        
        $current_user = wp_get_current_user();
        
        // Get API key
        $api_key = VMPFence_Config::get('central_api_key');
        
        if (empty($api_key)) {
            return '<div class="vmpfence-2fa-shortcode">' . 
                   '<p class="error">' . __('2FA service is not configured. Please contact the site administrator.', 'vmpfence-security') . '</p>' .
                   '</div>';
        }
        
        // Get current 2FA status
        $api_client = new VMPFence_API_Client();
        $status = $api_client->get_2fa_status($api_key, $current_user->ID);
        
        $is_enabled = false;
        if (!is_wp_error($status) && isset($status['enabled'])) {
            $is_enabled = $status['enabled'];
        }
        
        // Start output buffering
        ob_start();
        
        // Include template
        include VMPFENCE_PLUGIN_DIR . 'templates/frontend-2fa-management.php';
        
        return ob_get_clean();
    }
}

// Initialize on init hook
add_action('init', array('VMPFence_2FA_Shortcode', 'init'), 10);
