define([
    'jquery',
    'mage/utils/wrapper',
    'Magento_Checkout/js/model/quote'
], function ($, wrapper, quote) {
    'use strict';

    console.log('🔧 SHIPPING FIELDS: Default Processor Mixin LOADED!');

    return function (target) {
        target.saveShippingInformation = wrapper.wrap(
            target.saveShippingInformation,
            function (originalAction) {
                console.log('🚢 Default Processor: Preparing to save');
                
                // Get shipping address from quote
                var shippingAddress = quote.shippingAddress();
                
                // Get field values from DOM
                var shippingDate = null;
                var shippingMessage = null;
                
                $('input[name="shipping_date"], textarea[name="shipping_message"]').each(function() {
                    var name = $(this).attr('name');
                    var value = $(this).val();
                    
                    if (name === 'shipping_date' && value) {
                        shippingDate = value;
                    }
                    if (name === 'shipping_message' && value) {
                        shippingMessage = value;
                    }
                });
                
                // Add to shipping address custom attributes
                if ((shippingDate || shippingMessage) && shippingAddress) {
                    if (!shippingAddress.customAttributes) {
                        shippingAddress.customAttributes = {};
                    }
                    
                    if (shippingDate) {
                        shippingAddress.customAttributes.shipping_date = shippingDate;
                        console.log('✅ Added shipping_date to customAttributes:', shippingDate);
                    }
                    
                    if (shippingMessage) {
                        shippingAddress.customAttributes.shipping_message = shippingMessage;
                        console.log('✅ Added shipping_message to customAttributes:', shippingMessage);
                    }
                }
                
                return originalAction();
            }
        );
        
        return target;
    };
});
