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

    if (!human_resources_button || !human_resources_config) return;

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

        const human_resources_redirectUrl = human_resources_button.getAttribute('data-redirect') || human_resources_config.redirectUrl;

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

        const human_resources_installData = new FormData();
        human_resources_installData.append('action', 'install_and_activate_wordclever_plugin');
        human_resources_installData.append('nonce', human_resources_config.nonce);

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