<?php
/**
 * Copyright © Simplyin. All rights reserved.
 */
declare(strict_types=1);

namespace Simplyin\Payment\Observer;

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

/**
 * Copy pickup point extension attributes from Quote to Order
 *
 * Handles InPost, Orlen, and DPD pickup point attributes
 */
class CopyPickupPointToOrder implements ObserverInterface
{
    /**
     * @var LoggerInterface
     */
    protected LoggerInterface $logger;

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

    /**
     * Execute observer
     *
     * Copy extension attributes from Quote to Order for pickup point data
     *
     * @param Observer $observer
     * @return $this
     */
    public function execute(Observer $observer): static
    {
        try {
            $order = $observer->getEvent()->getOrder();
            $quote = $observer->getEvent()->getQuote();
            
            if (!$order || !$quote) {
                return $this;
            }

            // Copy extension attributes from Quote to Order
            $quoteExtension = $quote->getExtensionAttributes();
            $orderExtension = $order->getExtensionAttributes();
            
            if (!$quoteExtension || !$orderExtension) {
                return $this;
            }

            $this->copyPickupPointAttributes($quoteExtension, $orderExtension);

            $order->setExtensionAttributes($orderExtension);

        } catch (\Exception $e) {
            $this->logger->error('SimplyIn: Error copying pickup point to order: ' . $e->getMessage());
        }

        return $this;
    }

    /**
     * Copy pickup point extension attributes from Quote to Order
     *
     * @param mixed $quoteExtension
     * @param mixed $orderExtension
     * @return void
     */
    private function copyPickupPointAttributes($quoteExtension, $orderExtension): void
    {
        // Copy InPost locker ID
        if (method_exists($quoteExtension, 'getInpostLockerId') &&
            $quoteExtension->getInpostLockerId()) {
            $orderExtension->setInpostLockerId($quoteExtension->getInpostLockerId());
            $this->logger->info('SimplyIn: Copied inpost_locker_id to order');
        }

        // Copy Orlen attributes
        if (method_exists($quoteExtension, 'getOrlenpaczkaDestinationCode') &&
            $quoteExtension->getOrlenpaczkaDestinationCode()) {
            $orderExtension->setOrlenpaczkaDestinationCode($quoteExtension->getOrlenpaczkaDestinationCode());
            $this->logger->info('SimplyIn: Copied orlenpaczka_destination_code to order');
        }

        if (method_exists($quoteExtension, 'getOrlenpaczkaPointDetails') &&
            $quoteExtension->getOrlenpaczkaPointDetails()) {
            $orderExtension->setOrlenpaczkaPointDetails($quoteExtension->getOrlenpaczkaPointDetails());
            $this->logger->info('SimplyIn: Copied orlenpaczka_point_details to order');
        }
    }
}
