<?php
/**
 * Turbo Template Onboarding Wizard
 * Full-screen wizard interface like Astra
 * 
 * phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound
 * phpcs:disable WordPress.Security.NonceVerification.Recommended
 */

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

// Security: Check user capabilities
if (!current_user_can('manage_options')) {
    wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'turbo-templates-library-for-elementor'));
}

$step = isset($_GET['ci']) ? absint(wp_unslash($_GET['ci'])) : 0;
$steps = array(
    0 => esc_html__('Welcome', 'turbo-templates-library-for-elementor'),
    1 => esc_html__('Website Type', 'turbo-templates-library-for-elementor'), 
    2 => esc_html__('Template Gallery', 'turbo-templates-library-for-elementor'),
    3 => esc_html__('Customization', 'turbo-templates-library-for-elementor'),
    4 => esc_html__('Features & Plugins', 'turbo-templates-library-for-elementor'),
    5 => esc_html__('Final Details', 'turbo-templates-library-for-elementor'),
    6 => esc_html__('Import Process', 'turbo-templates-library-for-elementor')
);

$wizard_nonce = wp_create_nonce('trstemp_wizard_step');

// ✅ STEP 1: Register and enqueue a dummy script handle
wp_register_script( 'trstemp-wizard-main-js', '', array('jquery'), '1.0.5', true );
wp_enqueue_script( 'trstemp-wizard-main-js' );

// ✅ STEP 2: Prepare JavaScript variables
$js_vars_main = array(
    'ajaxurl' => admin_url('admin-ajax.php'),
    'admin_url' => admin_url(),
    'wizard_url' => admin_url('admin.php?page=turbo-starter-templates'),
    'step3_url' => admin_url('admin.php?page=turbo-starter-templates&ci=3'),
    'current_step' => $step,
    'nonce' => $wizard_nonce,
    'strings' => array(
        'select_template_alert' => esc_html__('Please select a template before continuing.', 'turbo-templates-library-for-elementor'),
        'exit_confirm' => esc_html__('Are you sure you want to exit the setup wizard?', 'turbo-templates-library-for-elementor'),
        'continue_text' => esc_html__('Continue', 'turbo-templates-library-for-elementor'),
        'start_import_text' => esc_html__('Start Import', 'turbo-templates-library-for-elementor'),
        'back_text' => esc_html__('← Back', 'turbo-templates-library-for-elementor'),
    )
);

// ✅ STEP 3: Create the COMPLETE inline JavaScript
$inline_script_main = '(function($) {
    "use strict";
    
    // Global variables
    const trstempVarsMain = ' . wp_json_encode($js_vars_main) . ';
    
    // Function declarations
    function trstemp_closeTurboWizard() {
        if (confirm(trstempVarsMain.strings.exit_confirm)) {
            window.location.href = trstempVarsMain.wizard_url;
        }
    }
    
    function trstemp_nextStep() {
        const currentStep = trstempVarsMain.current_step;
        const nextStep = currentStep + 1;
        const nonce = trstempVarsMain.nonce;
        
        // Special handling for step 2 (template selection)
        if (currentStep === 2) {
            const selectedTemplate = sessionStorage.getItem("trstemp_template_id");
            const selectedTemplateName = sessionStorage.getItem("trstemp_template_name");
            
            if (!selectedTemplate) {
                alert(trstempVarsMain.strings.select_template_alert);
                return;
            }
            
            // Redirect to step 3 with template parameters
            window.location.href = trstempVarsMain.step3_url + 
                "&template=" + selectedTemplate + 
                "&name=" + encodeURIComponent(selectedTemplateName) + 
                "&_wpnonce=" + nonce;
            return;
        }
        
        if (nextStep <= 6) {
            window.location.href = trstempVarsMain.wizard_url + "&ci=" + nextStep + "&_wpnonce=" + nonce;
        }
    }
    
    function trstemp_previousStep() {
        const currentStep = trstempVarsMain.current_step;
        const prevStep = currentStep - 1;
        const nonce = trstempVarsMain.nonce;
        
        if (prevStep >= 0) {
            window.location.href = trstempVarsMain.wizard_url + "&ci=" + prevStep + "&_wpnonce=" + nonce;
        }
    }
    
    // Make functions globally available
    window.trstemp_closeTurboWizard = trstemp_closeTurboWizard;
    window.trstemp_nextStep = trstemp_nextStep;
    window.trstemp_previousStep = trstemp_previousStep;
    
    // Initialize on DOM ready
    $(document).ready(function() {
        // Auto-hide WordPress admin elements
        $("body").addClass("trstemp-wizard-active");
        $("#wpadminbar, #adminmenumain, #wpfooter").hide();
        
        // Update next button text based on step
        const nextBtn = document.getElementById("trstemp-next-btn");
        if (nextBtn && trstempVarsMain.current_step === 5) {
            nextBtn.textContent = trstempVarsMain.strings.start_import_text + " →";
        }
    });
    
})(jQuery);';

// ✅ STEP 4: Add the inline script properly using wp_add_inline_script()
wp_add_inline_script( 'trstemp-wizard-main-js', $inline_script_main );
?>

<?php
      $tt_logo = plugins_url('assets/images/turboTemplatesKit_logo.webp',__FILE__);
?>

<div id="trstemp-wizard-container" class="trstemp-wizard-fullscreen">
    <div class="trstemp-wizard-header">
        <div class="trstemp-wizard-logo">
             <img src="<?php echo esc_url($tt_logo); ?>" alt="Turbo Templates Logo">
        </div>
        
        <div class="trstemp-wizard-progress">
            <?php for($i = 1; $i <= 6; $i++): ?>
            <span class="trstemp-step <?php echo esc_attr($i < $step ? 'completed' : ($i == $step ? 'active' : '')); ?>"><?php echo esc_html($i); ?></span>
            <?php if($i < 6): ?><span class="trstemp-connector"></span><?php endif; ?>
            <?php endfor; ?>
        </div>
        
        <button class="trstemp-wizard-close" onclick="trstemp_closeTurboWizard()">×</button>
    </div>
    
    <div class="trstemp-wizard-content" id="trstemp-wizard-content">
        <?php
        switch($step) {
            case 0:
                include __DIR__ . '/steps/step-0.php';
                break;
            case 1:
                include __DIR__ . '/steps/step-1.php';
                break;
            case 2:
                include __DIR__ . '/steps/step-2.php';
                break;
            case 3:
                include __DIR__ . '/steps/step-3.php';
                break;
            case 4:
                include __DIR__ . '/steps/step-4.php';
                break;
            case 5:
                include __DIR__ . '/steps/step-5.php';
                break;
            case 6:
                include __DIR__ . '/steps/step-6.php';
                break;
            default:
                include __DIR__ . '/steps/step-0.php';
        }
        ?>
    </div>
    
    <div class="trstemp-wizard-footer">
        <?php if($step > 0): ?>
        <button class="trstemp-btn-secondary" onclick="trstemp_previousStep()">
            <?php echo esc_html__('← Back', 'turbo-templates-library-for-elementor'); ?>
        </button>
        <?php endif; ?>
        
        <div class="trstemp-wizard-footer-right">
            <?php if($step < 6): ?>
            <button class="trstemp-btn-primary" onclick="trstemp_nextStep()" id="trstemp-next-btn">
                <?php echo $step == 5 ? esc_html__('Start Import', 'turbo-templates-library-for-elementor') : esc_html__('Continue', 'turbo-templates-library-for-elementor'); ?> →
            </button>
            <?php endif; ?>
        </div>
    </div>
</div>