<?php

namespace Opencart\Extension\Alsendo\DTO\Entity;

class OrderPickupPointEntity
{
    public int $orderId;
    public array $pickupPoint = [];

    public static function fromArray(array $data): self
    {
        $dto = new self();

        $dto->orderId = (int) $data['order_id'];
        $dto->pickupPoint = json_decode($data['pickup_point'], true);

        return $dto;
    }

    public function toString(): string
    {
        if (empty($this->pickupPoint)) {
            return '';
        }

        $code = $this->pickupPoint['code'] ?? null;
        $name = $this->pickupPoint['name'] ?? null;

        if (empty($name) && empty($code)) {
            return 'Unknown';
        }

        if (empty($code)) {
            return $name;
        }

        if (empty($name)) {
            return $code;
        }

        return sprintf('%s - %s', $code, $name);
    }
}
