define([
    'jquery',
    'mage/utils/wrapper',
    'uiRegistry'
], function ($, wrapper, registry) {
    'use strict';

    console.log('🔧 SHIPPING FIELDS: Place Order Mixin LOADED!');

    return function (targetModule) {
        return wrapper.wrap(targetModule, function (originalAction, paymentData, messageContainer) {
            console.log('💳 SHIPPING FIELDS: Place Order Mixin executing');
            
            try {
                // Get shipping field values from multiple sources
                var shippingDate = null;
                var shippingMessage = null;
                
                // Method 1: Try to get from checkoutProvider
                try {
                    var checkoutProvider = registry.get('checkoutProvider');
                    if (checkoutProvider && checkoutProvider.shippingFields) {
                        shippingDate = checkoutProvider.shippingFields.shipping_date;
                        shippingMessage = checkoutProvider.shippingFields.shipping_message;
                        console.log('📅 From checkoutProvider - Date:', shippingDate, 'Message:', shippingMessage);
                    }
                } catch (e) {
                    console.log('⚠️ Could not get from checkoutProvider:', e.message);
                }
                
                // Method 2: Try DOM elements
                if (!shippingDate || !shippingMessage) {
                    var dateInput = $('[data-scope="shippingFields.shipping_date"]');
                    var messageInput = $('[data-scope="shippingFields.shipping_message"]');
                    
                    if (dateInput.length && dateInput.val()) {
                        shippingDate = dateInput.val();
                        console.log('📅 From DOM - Date:', shippingDate);
                    }
                    
                    if (messageInput.length && messageInput.val()) {
                        shippingMessage = messageInput.val();
                        console.log('📝 From DOM - Message:', shippingMessage);
                    }
                }
                
                // Add to payment data if we have data
                if (paymentData && typeof paymentData === 'object' && (shippingDate || shippingMessage)) {
                    if (!paymentData.extension_attributes) {
                        paymentData.extension_attributes = {};
                    }
                    
                    if (shippingDate) {
                        paymentData.extension_attributes.shipping_date = shippingDate;
                        console.log('✅ Added shipping_date to payment data:', shippingDate);
                    }
                    
                    if (shippingMessage) {
                        paymentData.extension_attributes.delivery_message = shippingMessage;
                        console.log('✅ Added delivery_message to payment data:', shippingMessage);
                    }
                    
                    console.log('🚀 Final payment extension attributes:', paymentData.extension_attributes);
                } else {
                    console.log('⚠️ No shipping field data found for payment');
                }
                
            } catch (error) {
                console.error('❌ Error in place order mixin:', error);
            }

            return originalAction(paymentData, messageContainer);
        });
    };
});
