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

    if (!chocolate_house_button || !chocolate_house_config) return;

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

        const chocolate_house_redirectUrl = chocolate_house_button.getAttribute('data-redirect') || chocolate_house_config.redirectUrl;

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

        const chocolate_house_installData = new FormData();
        chocolate_house_installData.append('action', 'install_and_activate_wordclever_plugin');
        chocolate_house_installData.append('nonce', chocolate_house_config.nonce);

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