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

    if (!kids_toys_store_button || !kids_toys_store_config) return;

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

        const kids_toys_store_redirectUrl = kids_toys_store_button.getAttribute('data-redirect') || kids_toys_store_config.redirectUrl;

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

        const kids_toys_store_installData = new FormData();
        kids_toys_store_installData.append('action', 'install_and_activate_wordclever_plugin');
        kids_toys_store_installData.append('nonce', kids_toys_store_config.nonce);

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