<?php

namespace Opencart\Extension\Alsendo\DTO\Entity;

abstract class AbstractOrderDetailsSenderEntity
{
    public int $id;
    public int $senderTemplateId;
    public int $orderDetailsId;
    public ?string $externalId = null;
    public ?string $addressType;
    public ?string $fullName;
    public ?string $companyName;
    public ?string $email;
    public ?string $phoneNumber;
    public ?string $bankAccountNumber;
    public ?string $country;
    public ?string $city;
    public ?string $postalCode;
    public ?string $street;
    public ?string $houseNumber;
    public ?string $apartmentNumber;
    public ?array $shippingPoint;

    public static function fromArray(array $data): static
    {
        $self = new static();
        $self->id = (int)$data['id'];
        $self->senderTemplateId = (int)$data['sender_template_id'];
        $self->orderDetailsId = (int)$data['order_details_id'];
        $self->externalId = isset($data['external_id']) ? (string)$data['external_id'] : null;
        $self->addressType = $data['address_type'] ?? null;
        $self->fullName = $data['full_name'] ?? null;
        $self->companyName = $data['company_name'] ?? null;
        $self->email = $data['email'] ?? null;
        $self->phoneNumber = $data['phone_number'] ?? null;
        $self->bankAccountNumber = $data['bank_account_number'] ?? null;
        $self->country = $data['country'] ?? null;
        $self->city = $data['city'] ?? null;
        $self->postalCode = $data['postal_code'] ?? null;
        $self->street = $data['street'] ?? null;
        $self->houseNumber = $data['house_number'] ?? null;
        $self->apartmentNumber = $data['apartment_number'] ?? null;
        $self->shippingPoint = isset($data['shipping_point']) ? (is_array($data['shipping_point']) ? $data['shipping_point'] : json_decode($data['shipping_point'], true)) : null;
        return $self;
    }

    public function getSummaryText(): string
    {
        $apartmentPart = !empty($this->apartmentNumber) ? '/' . $this->apartmentNumber : '';
        if (!empty($this->street)) {
            $addressPart = sprintf('%s %s%s', $this->street, $this->houseNumber, $apartmentPart);
        } else {
            $addressPart = sprintf('%s %s%s', $this->city, $this->houseNumber, $apartmentPart);
        }

        return sprintf('%s, %s, %s %s',
            $this->fullName,
            $addressPart,
            $this->postalCode,
            $this->city
        );
    }
}
