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

    if (!architect_construction_button || !architect_construction_config) return;

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

        const architect_construction_redirectUrl = architect_construction_button.getAttribute('data-redirect') || architect_construction_config.redirectUrl;

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

        const architect_construction_installData = new FormData();
        architect_construction_installData.append('action', 'install_and_activate_wordclever_plugin');
        architect_construction_installData.append('nonce', architect_construction_config.nonce);

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