<?php

namespace Opencart\Extension\Alsendo\DTO\Entity;

abstract class AbstractOrderDetailsPackageEntity
{
    public int $id;
    public int $orderDetailsPackageDetailsId;
    public ?int $templateId;
    public ?string $templateName;
    public ?int $packageHeight;
    public ?int $packageWidth;
    public ?int $packageLength;
    public ?float $packageWeight;
    public ?string $packageShipmentPackaging;
    public ?bool $nonStandard;

    public static function fromArray(array $data): static
    {
        $self = new static();
        $self->id = (int)$data['id'];
        $self->orderDetailsPackageDetailsId = (int)$data['order_details_package_details_id'];
        $self->templateId = isset($data['template_id']) ? (int)$data['template_id'] : null;
        $self->templateName = $data['template_name'] ?? null;
        $self->packageHeight = isset($data['package_height']) ? (int)$data['package_height'] : null;
        $self->packageWidth = isset($data['package_width']) ? (int)$data['package_width'] : null;
        $self->packageLength = isset($data['package_length']) ? (int)$data['package_length'] : null;
        $self->packageWeight = isset($data['package_weight']) ? (float)$data['package_weight'] : null;
        $self->packageShipmentPackaging = $data['package_shipment_packaging'] ?? null;
        $self->nonStandard = isset($data['non_standard']) ? (bool)$data['non_standard'] : null;
        return $self;
    }

    public function getSummaryText(): string
    {
        $templateName = $this->templateName ? sprintf('%s, ', $this->templateName) : '';

        $dimensions = sprintf('%sx%sx%s',
            $this->packageWidth,
            $this->packageLength,
            $this->packageHeight,
        );

        $weight = $this->packageWeight;
        $shipmentPackaging = $this->packageShipmentPackaging;

        return sprintf('%s%scm, %skg, %s',
            $templateName,
            $dimensions,
            $weight,
            $shipmentPackaging
        );
    }
}
