<?php

declare(strict_types=1);

namespace Gls\GlsPoland\Entity;

use Doctrine\ORM\Mapping as ORM;
use Gls\GlsPoland\AdePlus\ParcelShop\ParcelShop;
use Gls\GlsPoland\Repository\OrderParcelShopRepository;

/**
 * @ORM\Entity(repositoryClass=OrderParcelShopRepository::class)
 * @ORM\Table(name=OrderParcelShop::TABLE_NAME)
 */
class OrderParcelShop
{
    /**
     * @internal
     */
    public const TABLE_NAME = _DB_PREFIX_ . 'gls_poland_order_parcel_shop';

    /**
     * @ORM\Id()
     * @ORM\Column(type="integer", name="id_order")
     */
    private $orderId;

    /**
     * @ORM\Column(type="string", length=32, name="parcel_shop_id")
     */
    private $parcelShopId;

    /**
     * @ORM\Column(type="string", length=255, name="name")
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=255, name="street", nullable=true)
     */
    private $street;

    /**
     * @ORM\Column(type="string", length=32, name="postal_code", nullable=true)
     */
    private $postalCode;

    /**
     * @ORM\Column(type="string", length=128, name="city", nullable=true)
     */
    private $city;

    public function __construct(int $orderId, ParcelShop $parcelShop)
    {
        $this->orderId = $orderId;
        $this->updateFromParcelShop($parcelShop);
    }

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

    public function getParcelShopId(): string
    {
        return $this->parcelShopId;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function getStreet(): ?string
    {
        return $this->street;
    }

    public function getPostalCode(): ?string
    {
        return $this->postalCode;
    }

    public function getCity(): ?string
    {
        return $this->city;
    }

    public function updateFromParcelShop(ParcelShop $parcelShop): self
    {
        $this->parcelShopId = $parcelShop->getId();
        $this->name = trim(sprintf('%s %s %s', $parcelShop->getName1(), $parcelShop->getName2(), $parcelShop->getName3()));
        $this->street = $parcelShop->getStreet();
        $this->postalCode = $parcelShop->getZipCode();
        $this->city = $parcelShop->getCity();

        return $this;
    }
}
