<?php

namespace Opencart\Extension\Alsendo\OrderDetails;

require_once(DIR_EXTENSION . 'alsendo/src/OrderDetails/OrderDetailsPayloadBuilder.php');
require_once(DIR_EXTENSION . 'alsendo/src/DTO/Entity/AlsendoRegionEntity.php');

use Opencart\Extension\Alsendo\DTO\Entity\AlsendoRegionEntity;
use Opencart\System\Engine\Registry;

class CreateOrderDetails
{
    private Registry $registry;
    private OrderDetailsPayloadBuilder $payloadBuilder;

    public function __construct(Registry $registry)
    {
        $this->registry = $registry;
        $this->payloadBuilder = new OrderDetailsPayloadBuilder($registry);
    }

    public function createIfNotExists(int $orderId, bool $isCatalog, AlsendoRegionEntity $currentRegion): void
    {
        $this->registry->load->model('extension/alsendo/module/alsendo_order_details');
        $existing = $this->registry->model_extension_alsendo_module_alsendo_order_details->getOrderDetailsByOrderId($orderId);
        if ($existing !== null) {
            return;
        }

        $order = $this->getOrder($orderId);
        if (!$order) {
            return;
        }

        $senderData = $this->payloadBuilder->buildSenderData();
        $shippingAddressData = $this->payloadBuilder->buildShippingAddressData($order, $currentRegion);
        $packageData = $this->payloadBuilder->buildPackageData($order, $isCatalog, $currentRegion);
        $shipmentData = $this->payloadBuilder->buildShipmentData($order);
        $packageData = $this->preparePackageDataForInsert($packageData, $currentRegion);

        $orderDetailsId = $this->registry->model_extension_alsendo_module_alsendo_order_details->insertOrderDetails($orderId, $currentRegion->id);

        $this->registry->load->model('extension/alsendo/module/alsendo_order_details_sender');
        $this->registry->model_extension_alsendo_module_alsendo_order_details_sender->insertSender($orderDetailsId, $senderData);

        $this->registry->load->model('extension/alsendo/module/alsendo_order_details_shipping_address');
        $this->registry->model_extension_alsendo_module_alsendo_order_details_shipping_address->insertShippingAddress($orderDetailsId, $shippingAddressData);

        $this->registry->load->model('extension/alsendo/module/alsendo_order_details_package_details');
        $this->registry->load->model('extension/alsendo/module/alsendo_order_details_package');
        $packageDetailsRow = [
            'template_id' => $packageData['template_id'] ?? null,
            'template_name' => $packageData['template_name'] ?? null,
            'cod_value' => $packageData['cod_value'] ?? null,
            'declared_value' => $packageData['declared_value'] ?? null,
            'package_content' => $packageData['package_content'] ?? null,
            'pickup_request' => $packageData['pickup_request'] ?? null,
            'pickup_request_date' => $packageData['pickup_request_date'] ?? null,
            'pickup_type' => $packageData['pickup_type'] ?? null,
            'ship_via_pickup_point' => isset($packageData['ship_via_pickup_point']) ? ($packageData['ship_via_pickup_point'] ? '1' : '0') : null
        ];
        $packageDetailsId = $this->registry->model_extension_alsendo_module_alsendo_order_details_package_details->insertPackageDetails($orderDetailsId, $packageDetailsRow);
        foreach ($packageData['packages'] ?? [] as $pkg) {
            $this->registry->model_extension_alsendo_module_alsendo_order_details_package->insertPackage($packageDetailsId, $pkg);
        }

        $this->registry->load->model('extension/alsendo/module/alsendo_order_details_shipment');
        $this->registry->model_extension_alsendo_module_alsendo_order_details_shipment->insertShipment($orderDetailsId, $shipmentData);
    }

    protected function preparePackageDataForInsert(array $packageData, AlsendoRegionEntity $currentRegion): array
    {
        return $packageData;
    }

    private function getOrder(int $orderId): ?array
    {
        $db = $this->registry->get('db');
        $prefix = defined('DB_PREFIX') ? DB_PREFIX : '';
        $query = $db->query("SELECT * FROM `" . $prefix . "order` WHERE `order_id` = '" . (int)$orderId . "'");
        return $query->num_rows ? $query->row : null;
    }
}
