/**
 * =============================================================================
 * @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        Licensing JS
 *
 * =============================================================================
 * 
 * || INDEX:
 * ||   - VALIDATE PRODUCT
 * || 
 * =============================================================================
 */

jQuery(function ($) {
    $(document)
    
/*
============================================================
    VALIDATE PRODUCT
============================================================
*/    
           
    // =========== Verification Key =========== //
    
        .on('click', '#submit', function (e) {
            e.preventDefault();
            
            const licenseKey = $('#license_key').val();
            
            $.ajax({
                url: blueRavenLicense.ajaxurl,
                type: 'POST',
                data: {
                    action: 'blue_raven_settings_license',
                    subaction: 'verify key',
                    key: licenseKey,
                    nonce: blueRavenLicense.nonce
                },
                beforeSend: function() {
                    $("#submit").prop('disabled', true);
                },
                success: function(response) {

    // =========== Remove any existing status messages =========== //

                    $('.license-status').remove();
                    
    // =========== Valid License =========== //

                    if (response.success) { $('.license-key-container').append( '<p class="license-status valid"><span class="dashicons dashicons-yes"></span> License is valid</p>');} else {

    // =========== Parse the response to get the status =========== //

                        const statusMatch = response.data.match(/\[status\] => (\w+)/);
                        const status = statusMatch ? statusMatch[1] : 'Invalid';
                        
                        let statusMessage = '';
                        let statusClass = 'invalid';
                        
                        switch(status) {
                            case 'Suspended':
                                statusMessage = 'License is suspended';
                                statusClass = 'suspended';
                                break;
                            case 'Expired':
                                statusMessage = 'License has expired';
                                statusClass = 'expired';
                                break;
                            default:
                                statusMessage = 'License is invalid';
                                statusClass = 'invalid';
                        }
                        
                        $('.license-key-container').append( `<p class="license-status ${statusClass}"><span class="dashicons dashicons-no"></span> ${statusMessage}</p>`);
                    }
                },
                complete: function() {
                    $("#submit").prop('disabled', false);
                    $('#user').trigger('click');
                },
                error: function(xhr, status, error) {
                    console.error('AJAX Error:', status, error);
                    $('.license-status').remove();
                    $('.license-key-container').append( '<p class="license-status error"><span class="dashicons dashicons-warning"></span> Verification failed</p>');
                    $('#user').trigger('click');
                }
            });
        })
        
    // =========== Hide / Show Key =========== //
    
        .on('click', '#toggleLicenseKey', function() {
            const $passwordInput = $('#license_key');
            const $icon = $(this);
            
            if ($passwordInput.attr('type') === 'password') {
                $passwordInput.attr('type', 'text');
                $icon.removeClass('fa-eye-slash').addClass('fa-eye');
            } else {
                $passwordInput.attr('type', 'password');
                $icon.removeClass('fa-eye').addClass('fa-eye-slash');
            }
        });
});