<?php

namespace Opencart\Extension\Alsendo\DTO\Unified;

use Opencart\Extension\Alsendo\DTO\Entity\OrderDetailsEntity;

require_once(DIR_EXTENSION . 'alsendo/src/DTO/Unified/SimpleSubmittedOrderPackageDetailsDTO.php');
require_once(DIR_EXTENSION . 'alsendo/src/DTO/Entity/OrderDetailsEntity.php');


class SimpleSubmittedOrderDetailsDTO
{
    public ?string $status;
    public ?string $shippingMethod;
    public ?string $serviceName;
    /** @var SimpleSubmittedOrderPackageDetailsDTO[] */
    public array $packages = [];
    public ?string $trackingPageUrl;
    /** @var array<string, mixed> */
    public array $waybillNumbers = [];

    public ?string $senderSummary;
    public ?string $recipientSummary;
    public ?string $packageSummary;
    public ?string $orderShipmentId;
    public ?string $pickupPointText = null;

    public static function fromOrderShipmentAndDetailsArray(array $orderShipment, OrderDetailsEntity $orderDetails, array $openCartData, ?string $currentRegionCode = null): self
    {
        $dto = new self();

        if ($currentRegionCode === 'czechia') {
            $dto->orderShipmentId = $orderShipment['data']['waybillNumbers'][0];
        } else {
            $dto->orderShipmentId = $orderShipment['data']['id'];
        }
        $dto->status = $orderShipment['status'];
        $dto->senderSummary = $orderDetails->sender?->getSummaryText();
        $dto->recipientSummary = $orderDetails->shippingAddress?->getSummaryText();

        if (array_key_exists('trackingUrl', $orderShipment['data'])) {
            $dto->trackingPageUrl = $orderShipment['data']['trackingUrl'];
        }

        if (array_key_exists('additional', $orderShipment['data'])) {

            foreach ($orderShipment['data']['additional'] as $key => $additional) {
                if (!empty($additional['trackingUrl'])) {
                    $dto->trackingPageUrl = $additional['trackingUrl'];
                }
            }
        }

        $dto->shippingMethod = $orderDetails->shipment?->shippingMethodName;
        $dto->serviceName = $orderShipment['data']['serviceName'];

        if (!empty($orderShipment['data']['waybillNumbers'])) {
            $dto->waybillNumbers = $orderShipment['data']['waybillNumbers'];
        }

        if (isset($orderShipment['data']['additional'])) {
            foreach ($orderShipment['data']['additional'] as $key => $additional) {
                if (!empty($additional['waybillNumber'])) {
                    $dto->waybillNumbers[] = $additional['waybillNumber'];
                }
            }
        }

        $dto->waybillNumbers = array_unique($dto->waybillNumbers);

        $dto->packages = SimpleSubmittedOrderPackageDetailsDTO::multipleFromOrderShipment($orderShipment);

        if (!empty($orderShipment['data']['trackingUrls']) && is_array($orderShipment['data']['trackingUrls']) && count($orderShipment['data']['trackingUrls']) >= 1) {
            $dto->trackingPageUrl = $orderShipment['data']['trackingUrls'][0];
        }

        return $dto;
    }

    public function getWaybillNumbersAsString(): string
    {
        return implode(',', $this->waybillNumbers);
    }
}