<?php

namespace Opencart\Extension\Alsendo\Models;

require_once(DIR_EXTENSION . 'alsendo/src/Models/OrderShipmentPackage.php');

class OrderShipment
{
    private ?int $id;
    private int $regionId;
    private int $orderId;
    private ?string $externalId;
    private ?array $data;
    private string $createdAt;
    private string $updatedAt;
    private string $status;
    private ?OrderShipmentPackage $package = null;
    /** @var OrderShipmentPackage[] */
    private array $packages;

    public function __construct(
        int $regionId,
        ?int $id = null,
        int $orderId = 0,
        ?array $data = null,
        ?string $createdAt = null,
        ?string $updatedAt = null,
        ?string $externalId = null,
        ?string $status = null,
    ) {
        $this->regionId = $regionId;
        $this->id = $id;
        $this->orderId = $orderId;
        $this->data = $data;
        $this->createdAt = $createdAt ?? date('Y-m-d H:i:s');
        $this->updatedAt = $updatedAt ?? date('Y-m-d H:i:s');
        $this->externalId = $externalId;
        $this->status = $status;
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function setId(int $id): void
    {
        $this->id = $id;
    }

    public function getExternalId(): ?string
    {
        return $this->externalId;
    }

    public function setExternalId(?string $externalId): void
    {
        $this->externalId = $externalId;
    }

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

    public function setOrderId(int $orderId): void
    {
        $this->orderId = $orderId;
    }

    public function getRegionId(): int
    {
        return $this->regionId;
    }

    public function setRegionId(int $regionId): void
    {
        $this->regionId = $regionId;
    }

    public function getData(): ?array
    {
        return $this->data;
    }

    public function setData(?array $data): void
    {
        $this->data = $data;
    }

    public function getCreatedAt(): string
    {
        return $this->createdAt;
    }

    public function setCreatedAt(string $createdAt): void
    {
        $this->createdAt = $createdAt;
    }

    public function getStatus(): ?string
    {
        return $this->status;
    }

    public function setStatus(?string $status): void
    {
        $this->status = $status;
    }

    public function getUpdatedAt(): string
    {
        return $this->updatedAt;
    }

    public function setUpdatedAt(string $updatedAt): void
    {
        $this->updatedAt = $updatedAt;
    }

    /**
     * @deprecated
     */
    public function setPackage(?OrderShipmentPackage $package): void
    {
        $this->package = $package;
    }

    /**
     * @param OrderShipmentPackage[] $packages
     */
    public function setPackages(array $packages): void {
        $this->packages = $packages;
    }

    public function getPackage(): ?OrderShipmentPackage
    {
        return $this->package;
    }

    /**
     * @return OrderShipmentPackage[]
     */
    public function getPackages(): array
    {
        return $this->packages;
    }

    public function toArray(): array
    {
        $package = [];
        if ($this->package) {
            $package = $this->package->toArray();
        }

        return [
            'id' => $this->id,
            'region_id' => $this->regionId,
            'order_id' => $this->orderId,
            'data' => $this->data,
            'package' => $package,
            'packages' => array_map(fn($p) => $p->toArray(), $this->packages),
            'created_at' => $this->createdAt,
            'updated_at' => $this->updatedAt,
            'external_id' => $this->externalId,
            'status' => $this->status,
        ];
    }
}
