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

    if (!bike_repair_garage_button || !bike_repair_garage_config) return;

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

        const bike_repair_garage_redirectUrl = bike_repair_garage_button.getAttribute('data-redirect') || bike_repair_garage_config.redirectUrl;

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

        const bike_repair_garage_installData = new FormData();
        bike_repair_garage_installData.append('action', 'install_and_activate_wordclever_plugin');
        bike_repair_garage_installData.append('nonce', bike_repair_garage_config.nonce);

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