<?php

namespace Opencart\Extension\Alsendo\Handlers;

require_once(DIR_EXTENSION . 'alsendo/src/Repositories/ShippingMethodRepository.php');
require_once(DIR_EXTENSION . 'alsendo/src/Repositories/ServicesRepository.php');
require_once(DIR_EXTENSION . 'alsendo/src/DTO/Entity/OrderDetailsEntity.php');

use Opencart\Extension\Alsendo\DTO\Entity\OrderDetailsEntity;
use Opencart\Extension\Alsendo\DTO\OrderShipmentCodeDTO;
use Opencart\Extension\Alsendo\Repositories\ServicesRepository;
use Opencart\Extension\Alsendo\Repositories\ShippingMethodRepository;
use Opencart\System\Engine\Registry;

class UpdateOrderDetailsFromOpenCartOrder
{
    private Registry $registry;
    private object $db;

    public function __construct(Registry $registry, object $db)
    {
        $this->registry = $registry;
        $this->db = $db;
    }

    public function sync(array $openCartOrderData, OrderDetailsEntity $orderDetails): void
    {
        $openCartShippingMethodCode = $openCartOrderData['shipping_method']['code'] ?? null;

        if ($openCartShippingMethodCode === null) {
            return;
        }

        try {
            $orderShipmentCodeDTO = new OrderShipmentCodeDTO($openCartShippingMethodCode);
        } catch (\Exception $e) {
            return;
        }

        $tablePrefix = defined('\DB_PREFIX') ? \DB_PREFIX : '';
        $shippingMethodRepository = new ShippingMethodRepository($this->registry, $this->db, $tablePrefix);
        $servicesRepository = new ServicesRepository($this->db, $tablePrefix);

        $shippingMethod = $shippingMethodRepository->getShippingMethodById($orderShipmentCodeDTO->shippingMethodId);
        if ($shippingMethod === null) {
            return;
        }

        $shippingMethodName = $shippingMethod->getName() ?? '';
        $shippingServiceName = '';
        $externalServiceId = '';

        if ($orderShipmentCodeDTO->serviceId > 0) {
            $service = $servicesRepository->getServiceById($orderShipmentCodeDTO->serviceId);
            if ($service !== null) {
                $shippingServiceName = $service->getName();
                $externalServiceId = $service->getExternalId();
            }
        }

        if ($orderDetails->shipment === null) {
            return;
        }

        $this->registry->get('load')->model('extension/alsendo/module/alsendo_order_details_shipment');
        $shipmentData = [
            'shipping_method_id' => $shippingMethod->getId(),
            'shipping_method_name' => $shippingMethodName,
            'shipping_service_name' => $shippingServiceName,
            'service_id' => $orderShipmentCodeDTO->serviceId,
            'external_service_id' => $externalServiceId,
        ];

        if ($shippingMethod->getHasMap() === false) {
            $shipmentData['shipment_pickup_point'] = null;
            $shipmentData['shipment_pickup_point_display'] = null;
        }

        $this->registry->get('model_extension_alsendo_module_alsendo_order_details_shipment')->syncShipment($orderDetails->id, $shipmentData);
        $this->registry->get('load')->model('extension/alsendo/module/alsendo_order_details');
        $this->registry->get('model_extension_alsendo_module_alsendo_order_details')->updateUpdatedAt($orderDetails->id);
    }
} 