<?php

namespace Opencart\Extension\Alsendo\DTO;

class OrderShipmentCodeDTO
{
    public string $codeName;
    public int $shippingMethodId;
    public int $serviceId;

    public function __construct(string $code)
    {
        if (!preg_match('/^alsendo\.([a-zA-Z0-9_]+):(\d+):(\d+)$/', $code, $matches)) {
            throw new \Exception("Invalid code format: $code");
        }

        $this->codeName = $matches[1];
        $this->shippingMethodId = (int)$matches[2];
        $this->serviceId = (int)$matches[3];

        if ($this->codeName === '' || $this->shippingMethodId <= 0 || $this->serviceId < 0) {
            throw new \Exception("Invalid code values: $code");
        }
    }
    public function __toString(): string
    {
        return sprintf('alsendo.%s:%d:%d', $this->codeName, $this->shippingMethodId, $this->serviceId);
    }
} 