// diyseo-script.js
jQuery(document).ready(function($) {
    function setLicenseKey() {
        var licenseKey = localStorage.getItem('diyseoLicK');
        console.log("Retrieved license key from localStorage:", licenseKey ? "Yes" : "No");
        var nonce = diyseo_ajax_object.nonce;
        
        if (licenseKey) {
            $.ajax({
                url: diyseo_ajax.ajax_url,
                type: 'POST',
                data: {
                    action: 'diyseo_set_license_key',
                    license_key: licenseKey,
                    _ajax_nonce: nonce // Pass the nonce
                },
                success: function(response) {
                    if (response.success) {
                        console.log('License key set successfully');
                        toggleMetaBoxes(true);
                    } else {
                        console.error('Failed to set license key:', response.data);
                        toggleMetaBoxes(false);
                    }
                },
                error: function(xhr, status, error) {
                    console.error('AJAX error:', error);
                    toggleMetaBoxes(false);
                }
            });
        } else {
            console.error('No license key found in localStorage');
            toggleMetaBoxes(false);
        }
    }

    function toggleMetaBoxes(show) {
        var metaBoxes = $('#diyseo_meta_box, #diyseo_meta_description_box, #diyseo_meta_title_box, #diyseo_featured_image_box');
        if (show) {
            metaBoxes.show();
        } else {
            metaBoxes.hide();
        }
    }

    function checkLicenseKey(callback) {
        var licenseKey = localStorage.getItem('diyseoLicK');
        if (licenseKey) {
            $.ajax({
                url: diyseo_ajax.ajax_url,
                type: 'POST',
                data: {
                    action: 'diyseo_check_license_key'
                },
                success: function(response) {
                    if (response.success) {
                        callback(response.data.is_set);
                    } else {
                        console.error('Failed to check license key');
                        callback(false);
                    }
                },
                error: function(xhr, status, error) {
                    console.error('AJAX error:', error);
                    callback(false);
                }
            });
        } else {
            callback(false);
        }
    }

    // Call setLicenseKey and toggle meta boxes when the page loads
    setLicenseKey();

    // Modify the click handler for the generate article button
    $('#diyseo_preview_button').on('click', function(e) {
        e.preventDefault();
        checkLicenseKey(function(isSet) {
            if (isSet) {
                // License key is set, proceed with content generation
                generatePreview();
            } else {
                // License key is not set, try setting it again
                setLicenseKey();
                alert('Please activate the plugin and refresh the page to use this feature.');
            }
        });
    });

    function generatePreview() {
        console.log("Generating preview...");
        // Your existing preview generation code here
        var postData = {
            action: 'diyseo_preview_content',
            diyseo_post_title: $('#diyseo_post_title').val(),
            diyseo_post_date: $('#diyseo_post_date').val(),
            diyseo_word_count: $('#diyseo_word_count').val(),
            diyseo_nonce: diyseo_ajax.nonce
        };

        $.post(diyseo_ajax.ajax_url, postData, function(response) {
            console.log("Preview generation response:", response);
            // Handle the response here
        }).fail(function(jqXHR, textStatus, errorThrown) {
            console.error("Preview generation failed:", textStatus, errorThrown);
        });
    }
});