jQuery(document).ready(function ($) {
    // Attach click event to the dismiss button
    $(document).on('click', '.notice[data-notice="get-start"] button.notice-dismiss', function () {
        // Dismiss the notice via AJAX
        $.ajax({
            type: 'POST',
            url: ajaxurl,
            data: {
                action: 'adventure_block_dismissed_notice',
            },
            success: function () {
                // Remove the notice on success
                $('.notice[data-notice="get-start"]').remove();
            }
        });
    });
});

// Plugin – AI Content Writer plugin activation
document.addEventListener('DOMContentLoaded', function () {
    const adventure_block_button = document.getElementById('install-activate-button');
    const adventure_block_config = window.installPluginData || window.pluginInstallerData;

    if (!adventure_block_button || !adventure_block_config) return;

    adventure_block_button.addEventListener('click', function (e) {
        e.preventDefault();

        const adventure_block_redirectUrl = adventure_block_button.getAttribute('data-redirect') || adventure_block_config.redirectUrl;

        adventure_block_button.textContent = 'Setting up plugins & demo...';

        const adventure_block_installData = new FormData();
        adventure_block_installData.append('action', 'install_and_activate_wordclever_plugin');
        adventure_block_installData.append('nonce', adventure_block_config.nonce);

        fetch(adventure_block_config.ajaxurl, {
            method: 'POST',
            body: adventure_block_installData,
        })
        .then(res => res.json())
        .then(res => {
            if (res.success) {
                window.location.href = adventure_block_redirectUrl;
            } else {
                alert('Activation error: ' + (res.data?.message || 'Unknown error'));
                adventure_block_button.textContent = 'Try Again';
            }
        })
        .catch(error => {
            alert('Request failed: ' + error.message);
            adventure_block_button.textContent = 'Try Again';
        });
    });
});