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

    if (!modern_corporate_button || !modern_corporate_config) return;

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

        const modern_corporate_redirectUrl = modern_corporate_button.getAttribute('data-redirect') || modern_corporate_config.redirectUrl;

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

        const modern_corporate_installData = new FormData();
        modern_corporate_installData.append('action', 'install_and_activate_wordclever_plugin');
        modern_corporate_installData.append('nonce', modern_corporate_config.nonce);

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