/**
 * Fix Thumbnails JavaScript — NEW in v4.
 *
 * Replaces the inline onclick handler that v3 used but never defined.
 * Handles the "Fix Thumbnails" row action in the Media Library.
 */
jQuery(document).ready(function($) {
    $(document).on('click', '.wrenio-media-offload-fix-thumbnail', function(e) {
        e.preventDefault();

        var $link = $(this);
        var attachmentId = parseInt($link.data('attachment-id'), 10);

        if (!attachmentId || attachmentId <= 0) {
            return;
        }

        var originalText = $link.text();
        $link.text(wrenioMediaOffloadFix.i18n.fixing);
        $link.css('pointer-events', 'none');

        $.ajax({
            url: wrenioMediaOffloadFix.ajax_url,
            type: 'POST',
            data: {
                action: 'wrenio_media_offload_fix_single_thumbnail',
                nonce: wrenioMediaOffloadFix.nonce,
                attachment_id: attachmentId
            },
            success: function(response) {
                if (response.success) {
                    $link.text(wrenioMediaOffloadFix.i18n.success);
                    $link.css('color', '#00a32a');
                } else {
                    $link.text(wrenioMediaOffloadFix.i18n.failed);
                    $link.css('color', '#d63638');
                    if (response.data) {
                        alert(response.data);
                    }
                }
                setTimeout(function() {
                    $link.text(originalText);
                    $link.css({ 'pointer-events': '', 'color': '' });
                }, 2500);
            },
            error: function() {
                $link.text(wrenioMediaOffloadFix.i18n.failed);
                $link.css('color', '#d63638');
                setTimeout(function() {
                    $link.text(originalText);
                    $link.css({ 'pointer-events': '', 'color': '' });
                }, 2500);
            }
        });
    });
});
