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: 'wardrobe_fashion_stylist_dismissed_notice',
            },
            success: function () {
                // Remove the notice on success
                $('.notice[data-notice="example"]').remove();
            }
        });
    });
});

// WordClever – AI Content Writer plugin activation
document.addEventListener('DOMContentLoaded', function () {
    const wardrobe_fashion_stylist_button = document.getElementById('install-activate-button');

    if (!wardrobe_fashion_stylist_button) return;

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

        const wardrobe_fashion_stylist_redirectUrl = wardrobe_fashion_stylist_button.getAttribute('data-redirect');

        // Step 1: Check if plugin is already active
        const wardrobe_fashion_stylist_checkData = new FormData();
        wardrobe_fashion_stylist_checkData.append('action', 'check_wordclever_activation');

        fetch(installWordcleverData.ajaxurl, {
            method: 'POST',
            body: wardrobe_fashion_stylist_checkData,
        })
        .then(res => res.json())
        .then(res => {
            if (res.success && res.data.active) {
                // Plugin is already active → just redirect
                window.location.href = wardrobe_fashion_stylist_redirectUrl;
            } else {
                // Not active → proceed with install + activate
                wardrobe_fashion_stylist_button.textContent = 'Installing & Activating...';

                const wardrobe_fashion_stylist_installData = new FormData();
                wardrobe_fashion_stylist_installData.append('action', 'install_and_activate_wordclever_plugin');
                wardrobe_fashion_stylist_installData.append('_ajax_nonce', installWordcleverData.nonce);

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

