<?php

namespace Opencart\Extension\Alsendo\Models;

class OrderPickupPoint
{
    private int $orderId;
    private string $pickupPoint;

    public function __construct(int $orderId, string $pickupPoint)
    {
        $this->orderId = $orderId;
        $this->pickupPoint = $pickupPoint;
    }

    public function getOrderId(): int
    {
        return $this->orderId;
    }

    public function getPickupPoint(): array
    {
        return json_decode($this->pickupPoint, true) ?? [];
    }

    public function getCode(): ?string
    {
        $pickupPointData = json_decode($this->pickupPoint, true);

        return $pickupPointData['code'] ?? null;
    }

    public function getName(): ?string
    {
        $code = $this->getCode() ?? null;
        $name = $this->getPickupPoint()['name'] ?? null;

        if ($code && $name) {
            return sprintf('%s - %s', $this->getCode(), $this->getPickupPoint()['name']);
        }

        return null;
    }

    public function toArray(): array
    {
        return [
            'order_id' => $this->orderId,
            'pickup_point' => json_decode($this->pickupPoint, true)
        ];
    }
}
