<?php

namespace KupShop\GraphQLBundle\ApiPos\Types\Order;

use KupShop\GraphQLBundle\ApiAdmin\Types\Order\OrderItem;
use KupShop\GraphQLBundle\ApiPos\Types\Collection\PosSerialNumberCollection;
use KupShop\GraphQLBundle\ApiPos\Types\Purchase\PosSerialNumber;
use KupShop\GraphQLBundle\ApiPos\Types\Warehouse\PosStoreItemCollection;
use KupShop\GraphQLBundle\ApiPos\Types\Warehouse\PosWarehouseCollection;
use KupShop\OrderingBundle\OrderList\OrderCollection;
use TheCodingMachine\GraphQLite\Annotations\Field;
use TheCodingMachine\GraphQLite\Annotations\Type;

/**
 * @Type()
 */
class PosOrderItem extends OrderItem
{
    protected $serialNumbers;

    private ?PosStoreItemCollection $stores;
    private ?PosWarehouseCollection $warehouse;

    public function __construct(\KupShop\OrderingBundle\Entity\Order\OrderItem $item, $stores = null, $warehouse = null, ?OrderCollection $orderCollection = null)
    {
        parent::__construct($item, $orderCollection);

        if (findModule(\Modules::PRODUCTS_SERIAL_NUMBERS)) {
            $serialNumbers = [];
            $noteSerialNumbers = $item->getNote()['serial_numbers'] ?? [];
            foreach ($noteSerialNumbers as $key => $serialNumber) {
                $serialNumbers[] = new PosSerialNumber($key, true);
            }
            if (sizeof($serialNumbers) > 0) {
                $this->serialNumbers = new PosSerialNumberCollection($serialNumbers);
            }
        }

        $this->stores = $stores;
        $this->warehouse = $warehouse;
    }

    /**
     * Seriová čísla přiřazená k produktu.
     *
     * @Field()
     */
    public function getSerialNumbers(): ?PosSerialNumberCollection
    {
        return $this->serialNumbers ?? null;
    }

    /**
     * @Field()
     */
    public function getStores(): ?PosStoreItemCollection
    {
        return $this->stores;
    }

    /**
     * @Field()
     */
    public function getWarehouse(): ?PosWarehouseCollection
    {
        return $this->warehouse;
    }
}
