jQuery(document).ready(function($) {
    $(document).on('click', '.notice[id^="freshenup-"] .notice-dismiss', function() {
        var notice = $(this).closest('.notice');
        var noticeId = notice.data('notice-id');
        var nonce = notice.data('nonce');
        
        if (noticeId && nonce) {
            $.ajax({
                url: freshenupAjax.ajaxurl,
                type: 'POST',
                data: {
                    action: 'freshenup_dismiss_notice',
                    notice_id: noticeId,
                    nonce: nonce
                },
                success: function(response) {
                    if (response.success) {
                        notice.fadeOut();
                    }
                },
                error: function() {
                    console.log('Error dismissing notice');
                }
            });
        }
    });
});

jQuery(document).ready(function($) {
    $('#freshenup-check-api-key').on('click', function() {
        var $button = $(this);
        var $result = $('#freshenup-api-key-result');
        var apiKey = $('#freshenup_openai_api_key').val();
        var provider = $('#freshenup_ai_api_provider').length ? $('#freshenup_ai_api_provider').val() : 'openai';

        if (!apiKey || apiKey.trim() === '') {
            $result.html('<span style="color:red">' + freshenupAjax.invalid_key_text + ' No API key entered.</span>');
            return;
        }
        
        $button.prop('disabled', true).text(freshenupAjax.checking_text);
        $result.html('');
        
        $.ajax({
            url: freshenupAjax.ajaxurl,
            type: 'POST',
            data: {
                action: 'freshenup_check_api_key',
                nonce: freshenupAjax.check_api_key_nonce,
                api_key: apiKey,
                provider: provider
            },
            success: function(response) {
                if (response && response.success) {
                    $result.empty().append(
                        $('<span>').css('color', 'green').text(response.data.message)
                    );
                } else {
                    var errorMsg = (response && response.data && (response.data.error || response.data.message))
                        ? (response.data.error || response.data.message)
                        : freshenupAjax.ajax_error_text;
                    $result.empty().append(
                        $('<span>').css('color', 'red').text(freshenupAjax.invalid_key_text + ' ' + errorMsg)
                    );
                }
            },
            error: function(jqXHR, textStatus) {
                var detail = jqXHR && jqXHR.status ? ' (HTTP ' + jqXHR.status + ')' : (textStatus ? ' (' + textStatus + ')' : '');
                $result.empty().append(
                    $('<span>').css('color', 'red').text(freshenupAjax.ajax_error_text + detail)
                );
            },
            complete: function() {
                $button.prop('disabled', false).text(freshenupAjax.check_key_text);
            }
        });
    });
});