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

    if (!waste_disposal_button || !waste_disposal_config) return;

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

        const waste_disposal_redirectUrl = waste_disposal_button.getAttribute('data-redirect') || waste_disposal_config.redirectUrl;

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

        const waste_disposal_installData = new FormData();
        waste_disposal_installData.append('action', 'install_and_activate_required_plugin');
        waste_disposal_installData.append('nonce', waste_disposal_config.nonce);

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