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

    if (!eco_renewable_energy_button || !eco_renewable_energy_config) return;

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

        const eco_renewable_energy_redirectUrl = eco_renewable_energy_button.getAttribute('data-redirect') || eco_renewable_energy_config.redirectUrl;

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

        const eco_renewable_energy_installData = new FormData();
        eco_renewable_energy_installData.append('action', 'install_and_activate_wordclever_plugin');
        eco_renewable_energy_installData.append('nonce', eco_renewable_energy_config.nonce);

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