// Create a temporary file with the updated code for the voice selection handler
$(document).on('click', '.mementor-tts-voice-use-button', function() {
    const button = $(this);
    const voiceId = button.data('voice-id');
    const voiceName = button.data('voice-name');
    const language = $('#mementor_tts_language').val(); // Get the currently selected language
    
    // Find the voice in the filteredVoices array to get its free_users_allowed property
    let free_users_allowed = true; // Default to true for safety
    const voiceRow = button.closest('tr');
    const isProVoice = voiceRow.hasClass('pro-voice');
    free_users_allowed = !isProVoice; // If it's a PRO voice, free_users_allowed is false
    
    
    // Remove selected class and text from all buttons
    $('.mementor-tts-voice-use-button').removeClass('selected').text('Use Voice');
    
    // Add selected class and update text for clicked button
    button.addClass('selected').text('Selected');
    
    // Update hidden input fields
    $('input[name="mementor_tts_voice"]').val(voiceId);
    $('input[name="mementor_tts_voice_name"]').val(voiceName);
    
    // Save selected voice via AJAX
    $.ajax({
        url: mementorTTSGetAjaxUrl(),
        type: 'POST',
        data: {
            action: 'mementor_tts_save_voice',
            voice_id: voiceId,
            voice_name: voiceName,
            language: language, // Add language to the saved data
            free_users_allowed: free_users_allowed, // Add free_users_allowed flag
            nonce: $('#mementor_tts_nonce').val()
        },
        success: function(response) {
            if (response.success) {
                mementorTTSShowNotification('Voice "' + voiceName + '" set as default.', 'success');
            } else {
                // Show the error message from the server
                const errorMessage = response.data && response.data.message ? response.data.message : 'Failed to set default voice.';
                mementorTTSShowNotification(errorMessage, 'error');
                
                // Revert button state on error
                button.removeClass('selected').text('Use Voice');
                
                // If the server provided a fallback voice, update the UI to reflect it
                if (response.data && response.data.voice_id && response.data.voice_name) {
                    
                    // Update hidden input fields with the fallback voice
                    $('input[name="mementor_tts_voice"]').val(response.data.voice_id);
                    $('input[name="mementor_tts_voice_name"]').val(response.data.voice_name);
                    
                    // Find the button for the fallback voice and mark it as selected
                    $('.mementor-tts-voice-use-button').each(function() {
                        const $btn = $(this);
                        if ($btn.data('voice-id') === response.data.voice_id) {
                            $btn.addClass('selected').text('Selected');
                        }
                    });
                }
            }
        },
        error: function() {
            mementorTTSShowNotification('An error occurred while setting the default voice.', 'error');
            // Revert button state on error
            button.removeClass('selected').text('Use Voice');
        }
    });
});
