<?php
/**
 * Observer to handle shipping fields before order submission
 */

namespace PortalPeople\ShippingFields\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;

class QuoteSubmitBefore implements ObserverInterface
{
    /**
     * @var LoggerInterface
     */
    private $logger;

    /**
     * Constructor
     *
     * @param LoggerInterface $logger
     */
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    /**
     * Execute observer
     *
     * @param Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $this->logger->info('🔍 QuoteSubmitBefore Observer: Triggered');
        
        /** @var \Magento\Quote\Model\Quote $quote */
        $quote = $observer->getData('quote');
        
        /** @var \Magento\Sales\Model\Order $order */
        $order = $observer->getData('order');
        
        if ($quote && $order) {
            $shippingDate = $quote->getData('shipping_date');
            $shippingMessage = $quote->getData('shipping_message');
            
            $this->logger->info('📦 Quote data - Date: ' . $shippingDate . ', Message: ' . $shippingMessage);
            
            if ($shippingDate) {
                $order->setData('shipping_date', $shippingDate);
                $this->logger->info('✅ Set order shipping_date: ' . $shippingDate);
            }
            
            if ($shippingMessage) {
                $order->setData('shipping_message', $shippingMessage);
                $this->logger->info('✅ Set order shipping_message: ' . $shippingMessage);
            }
        }
    }
}
