jQuery(document).ready(function ($) {
    $('#generate-ai-prompt').on('click', function (e) {
        e.preventDefault();

        var $button = $(this);
        var $promptField = $('#smarter_wp_wc_search_prompt');

        $button.prop('disabled', true).text('Generating...');

        $.ajax({
            url: smarterWpWcSearchAjax.ajaxurl,
            type: 'POST',
            data: {
                action: 'smarwpwcsearch_generate_ai_prompt',
                nonce: smarterWpWcSearchAjax.nonce
            },
            success: function (response) {
                if (response && response.success && response.data && response.data.prompt) {
                    $promptField.val(response.data.prompt);
                } else {
                    alert('Failed to generate prompt. Please try again.');
                }
                $button.prop('disabled', false).text('Generate AI Prompt');
            },
            error: function () {
                alert('Error generating prompt. Please try again.');
                $button.prop('disabled', false).text('Generate AI Prompt');
            }
        });
    });
});