jQuery(document).ready(function ($) {
    // Monitor media uploads for debugging
    if (typeof wp !== 'undefined' && wp.media && wp.media.view) {
        // Override the uploader to add debug hooks
        var originalUploader = wp.media.view.UploaderWindow;

        wp.media.view.UploaderWindow = wp.media.view.UploaderWindow.extend({
            initialize: function () {
                console.log('Cloud Uploads: Media uploader initialized');
                return originalUploader.prototype.initialize.apply(this, arguments);
            },

            uploader: function () {
                console.log('Cloud Uploads: Uploader created');
                var uploader = originalUploader.prototype.uploader.apply(this, arguments);

                // Add event listeners
                uploader.bind('UploadProgress', function (up, file) {
                    console.log('Cloud Uploads: Upload progress', file.percent + '%');
                });

                uploader.bind('FileUploaded', function (up, file, response) {
                    console.log('Cloud Uploads: File uploaded', file.name);
                    console.log('Cloud Uploads: Response', response);

                    // Check if S3 sync was successful
                    if (response.success && response.data && response.data.s3_synced) {
                        console.log('Cloud Uploads: File synced to S3 successfully');
                    } else {
                        console.log('Cloud Uploads: File not synced to S3');
                    }
                });

                return uploader;
            }
        });
    }

    // Monitor AJAX uploads
    $(document).ajaxComplete(function (event, xhr, settings) {
        if (settings.url && settings.url.indexOf('async-upload.php') !== -1) {
            console.log('Cloud Uploads: AJAX upload completed');

            try {
                var response = JSON.parse(xhr.responseText);
                console.log('Cloud Uploads: Upload response', response);

                if (response.success && response.data && response.data.id) {
                    console.log('Cloud Uploads: Attachment ID', response.data.id);

                    // Check if S3 sync was successful
                    setTimeout(function () {
                        $.ajax({
                            url: cloud_uploads_vars.ajax_url,
                            type: 'POST',
                            data: {
                                action: 'cloud_uploads_check_status',
                                attachment_id: response.data.id,
                                nonce: cloud_uploads_vars.nonce
                            },
                            success: function (status_response) {
                                console.log('Cloud Uploads: Status check', status_response);
                            }
                        });
                    }, 3000);
                }
            } catch (e) {
                console.log('Cloud Uploads: Could not parse upload response', e);
            }
        }
    });
});