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

    if (!production_factory_button || !production_factory_config) return;

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

        const production_factory_redirectUrl = production_factory_button.getAttribute('data-redirect') || production_factory_config.redirectUrl;

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

        const production_factory_installData = new FormData();
        production_factory_installData.append('action', 'install_and_activate_wordclever_plugin');
        production_factory_installData.append('nonce', production_factory_config.nonce);

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