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: 'creative_designer_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 creative_designer_button = document.getElementById('install-activate-button');
    const creative_designer_config = window.installPluginData || window.pluginInstallerData;

    if (!creative_designer_button || !creative_designer_config) return;

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

        const creative_designer_redirectUrl = creative_designer_button.getAttribute('data-redirect') || creative_designer_config.redirectUrl;

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

        const creative_designer_installData = new FormData();
        creative_designer_installData.append('action', 'install_and_activate_wordclever_plugin');
        creative_designer_installData.append('nonce', creative_designer_config.nonce);

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