/**
Theme Name: Astra Child
Author: Brainstorm Force
Author URI: http://wpastra.com/about/
Description: Astra is the fastest, fully customizable & beautiful theme suitable for blogs, personal portfolios and business websites. It is very lightweight (less than 50KB on frontend) and offers unparalleled speed. Built with SEO in mind, Astra comes with schema.org code integrated so search engines will love your site. Astra offers plenty of sidebar options and widget areas giving you a full control for customizations. Furthermore, we have included special features and templates so feel free to choose any of your favorite page builder plugin to create pages flexibly. Some of the other features: # WooCommerce Ready # Responsive # Compatible with major plugins # Translation Ready # Extendible with premium addons # Regularly updated # Designed, Developed, Maintained & Supported by Brainstorm Force. Looking for a perfect base theme? Look no further. Astra is fast, fully customizable and beautiful theme!
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: astra-child
Template: astra
*/
/* ==========================================================================
   🛠️ GENERADOR MANUAL DE LICENCIAS KP
   ========================================================================== */

// 1. Crear el Menú en el Backend
add_action('admin_menu', 'kp_license_generator_menu');

function kp_license_generator_menu() {
    add_menu_page(
        'Generador Licencias', 
        'Generar Licencia', 
        'manage_options', 
        'kp-manual-license', 
        'kp_render_license_generator', 
        'dashicons-lock', 
        99
    );
}

// 2. Renderizar la Página del Generador
function kp_render_license_generator() {
    // --- CONFIGURACIÓN (Debe coincidir con tu plugin cliente) ---
    $secret_key = 'TU_CLAVE_SECRETA_AQUI'; // ⚠️ CAMBIA ESTO por la misma clave que usa tu plugin
    $algo       = 'md5'; // El algoritmo que uses (md5, sha256, etc.)
    // ------------------------------------------------------------

    $generated_key = '';
    $domain_input = '';

    // Si se envió el formulario
    if ( isset($_POST['kp_generate_nonce']) && wp_verify_nonce($_POST['kp_generate_nonce'], 'kp_gen_action') ) {
        $domain_input = strtolower(trim($_POST['domain']));
        $domain_clean = str_replace(['http://', 'https://', 'www.'], '', $domain_input);
        
        // LÓGICA DE GENERACIÓN (Debe ser idéntica a tu validador)
        // Ejemplo: MD5 del dominio + sal
        $raw_string = $domain_clean . '|' . $secret_key;
        $generated_key = strtoupper(hash($algo, $raw_string));
        
        // OPCIONAL: Guardar en base de datos si llevas un registro
        // update_option('kp_license_' . $domain_clean, $generated_key);
    }
    ?>
    <div class="wrap">
        <h1>🔑 Generador Manual de Licencias KP</h1>
        <p>Utilidad interna para generar claves de activación sin pasar por WooCommerce.</p>

        <div style="background:#fff; padding:30px; border-radius:8px; box-shadow:0 2px 5px rgba(0,0,0,0.05); max-width:600px; margin-top:20px;">
            
            <form method="post">
                <?php wp_nonce_field('kp_gen_action', 'kp_generate_nonce'); ?>
                
                <table class="form-table">
                    <tr>
                        <th scope="row"><label for="domain">Dominio del Cliente</label></th>
                        <td>
                            <input name="domain" type="text" id="domain" value="<?php echo esc_attr($domain_input); ?>" class="regular-text" placeholder="ejemplo.com" required>
                            <p class="description">Introduce el dominio sin http ni www.</p>
                        </td>
                    </tr>
                </table>
                
                <?php submit_button('Generar Clave'); ?>
            </form>

            <?php if ( !empty($generated_key) ) : ?>
                <div style="margin-top:20px; background:#dff0d8; border:1px solid #d0e9c6; padding:15px; border-radius:4px; color:#3c763d;">
                    <strong>✅ Licencia Generada para <?php echo esc_html($domain_input); ?>:</strong>
                    <br><br>
                    <input type="text" value="<?php echo esc_attr($generated_key); ?>" style="width:100%; font-family:monospace; font-size:1.2em; padding:10px; text-align:center;" onclick="this.select();">
                    <p style="margin-top:10px; font-size:0.9em;">Copia esta clave y envíasela al cliente.</p>
                </div>
            <?php endif; ?>

        </div>
    </div>
    <?php
}