<?php
 
 /**
 * =============================================================================
 * @package         Blue Raven (Free Version)
 * @Plugin URI      https://enterrahost.com/plugins/blue-raven
 * 
 * @Author          EnterraHost
 * @Author URI      https://enterrahost.com
 * @License         GPL-2.0-or-later
 *  
 * @Copyright       (c) 2025 EnterraHost
 * @Contents        Load Product Subpage
 *
 * =============================================================================
 * 
 * || INDEX:
 * ||   - BLUE RAVEN ADMIN LICENSE 
 * || 
 * =============================================================================
 */

if ( ! defined( 'ABSPATH' ) ) exit; 

/*
============================================================
    BLUE RAVEN ADMIN LICENSE 
============================================================
*/

class BlueRaven_Admin_License {
    
    /**
     * Renders the license validation interface.
     *
     * Entry point for displaying the license form in the admin UI.
     * Calls the internal `output_license_form()` method to render
     * the HTML structure for license entry and status feedback.
     *
     * @access public
     * @static
     * @return void
     */
    public static function render() {
        self::output_license_form();
    }
    
    /**
     * Outputs the HTML form for license validation.
     *
     * This includes the input fields for the license key and domain,
     * license status display, and submission button. It is intended
     * to be called internally by the render method.
     *
     * @access private
     * @static
     * @return void
     */
    private static function output_license_form() {
        global $wpdb;
        $table = $wpdb->prefix . 'BlueRavenConfig';
        
        $license_key = $wpdb->get_var($wpdb->prepare("SELECT value FROM $table WHERE setting = 'licenseKey' LIMIT 1" ));
        $license_status = $wpdb->get_var( $wpdb->prepare("SELECT value FROM $table WHERE setting = 'licensed' LIMIT 1" ));
        
        $message = '';
        $status = '';
        
        $status = isset($_GET['status']) ? sanitize_text_field(wp_unslash($_GET['status'])) : '';
        if ($status === 'error') {
            $message = 'Invalid Key, please recheck your key or contact Enterrahost';
        } elseif ($status === 'success') {
            $message = 'License validated successfully!';
        }

        ?>
            <div class='parent-container'>
                <h1><?php esc_html_e('Blue Raven License Validation', 'blue-raven'); ?></h1>

                <?php if (!empty($message)) : ?>
                    <div class="notice notice-<?php echo esc_attr(($status === 'success') ? 'success' : 'error'); ?>">
                        <p><?php echo esc_html($message); ?></p>
                    </div>
                <?php endif; ?>

                <form method="post" action="">
                    <?php wp_nonce_field('blue_raven_license_check'); ?>

                    <div style="margin: 10rem auto 5rem auto; width: 90%; max-width: 800px;" class='border-bottom-white'>
                        <div class='flex-sb'>
                            <div style="margin: 0; width: 70%;" class='spacing-right'>
                                <label class='text-center' for='license_key'>
                                    <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('License Key', 'blue-raven'); ?></p>
                                </label>
                                <div style="position: relative;">
                                    <input type='password' name='license_key' id='license_key' placeholder="<?php echo esc_attr(($license_status !== 'yes') ? __('Enter your Blue Raven license key', 'blue-raven') : ''); ?>" class='br-input' style='width:100%;' value="<?php echo esc_attr($license_key ?? ''); ?>"required>
                                    <i id="toggleLicenseKey" class="fas fa-eye-slash" style="padding: 0.5rem; position: absolute; right: 10px; top: 50%; transform: translateY(-50%); cursor: pointer; color: #666;" aria-label="<?php esc_attr_e('Toggle license key visibility', 'blue-raven'); ?>" role="button" tabindex="0" ></i>
                                </div>
                            </div>

                            <div class="license-key-container">
                                <?php if ($license_status === 'yes' || $license_status === 'valid') : ?>
                                    <p class="license-status valid"><span class="dashicons dashicons-yes"></span> <?php esc_html_e('License is valid', 'blue-raven'); ?></p>
                                <?php elseif ($license_status === 'suspended') : ?>
                                    <p class="license-status suspended"><span class="dashicons dashicons-no"></span> <?php esc_html_e('License is suspended', 'blue-raven'); ?></p>
                                <?php elseif ($license_status === 'expired') : ?>
                                    <p class="license-status expired"><span class="dashicons dashicons-no"></span> <?php esc_html_e('License has expired', 'blue-raven'); ?></p>
                                <?php elseif (!empty($license_key)) : ?>
                                    <p class="license-status invalid"><span class="dashicons dashicons-no"></span> <?php esc_html_e('License is invalid', 'blue-raven'); ?></p>
                                <?php else : ?>
                                    <p class="license-status missing"><span class="dashicons dashicons-warning"></span> <?php esc_html_e('License key missing', 'blue-raven'); ?></p>
                                <?php endif; ?>
                            </div>
                        </div>

                        <div style="margin: 0; width: 70%;" class='spacing-right'>
                            <label class='text-center' for='domain'>
                                <p class='no-margin-padding spacing-right-small text-center'><?php esc_html_e('Domain', 'blue-raven'); ?></p>
                            </label>
                            <input type="text" name="domain" id="domain" placeholder="<?php esc_attr_e('Domain', 'blue-raven'); ?>" class="br-input" style="width:100%;" value="<?php echo esc_attr(isset($_SERVER['SERVER_NAME']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])) : ''); ?>" required>
                        </div>

                        <div class='flex-right' style='margin: 1.5rem 0rem;'>
                            <input type="submit" name="submit" id="submit" class='enterra-button' value="<?php esc_attr_e('Validate License', 'blue-raven'); ?>" >
                        </div>
                    </div> 
                </form>
                <?php if (empty($license_key)) : ?>
                     <div style="margin: auto; width: 90%; max-width: 800px;">
                        <h3 class='no-margin-padding text-center' style='margin-bottom: 1.5rem;'><?php esc_html_e('It looks like you don\'t have a License yet ?', 'blue-raven'); ?></h3>
                        <div class='flex-center' style='margin: 1.5rem 0rem;'>
                            <a href='https://portal.enterrahost.com/store/blue-raven-wordpress-plugin' target='_blank' class='enterra-button'><?php esc_attr_e('Get a License', 'blue-raven'); ?></a>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        <?php
    }
}
