<?php

namespace External\VarioBundle\Synchronizers;

class SupplySynchronizer extends BaseSynchronizer
{
    protected static $type = 'otSupply';
    protected static $batch = 200;
    protected static $maxBatches = 20;

    protected $fields = [
        'Quantity' => 'store',
    ];

    public function getEshopID($objectID)
    {
        $storeId = $this->helper->getMapping('vario_stores', $objectID['id_store']);
        $productId = $this->helper->getMapping('vario_products', $objectID['id_product']);

        if (!$storeId || !$productId) {
            // we do not have product or supplier
            return null;
        }

        $variationId = $this->helper->findVariation($objectID['id_variation'], $productId);
        if ($variationId === false) {
            return null;
        }

        $storeItemId = $this->getStoreItemId($storeId, $productId, $variationId);

        $this->addChangedItem(['id_product' => $productId, 'id_variation' => $variationId]);

        return [
            'id' => $storeItemId,
            'id_product' => $productId,
            'id_variation' => $variationId,
        ];
    }

    public function getStoreItemId($storeId, $productId, $variationId): int
    {
        $id = $this->selectSQL(
            'stores_items',
            [
                'id_product' => $productId,
                'id_variation' => $variationId,
                'id_store' => $storeId,
            ],
            ['id']
        )->fetchColumn();

        if (!$id) {
            $id = sqlGetConnection()->transactional(function () use ($storeId, $productId, $variationId) {
                sqlQueryBuilder()
                    ->insert('stores_items')
                    ->directValues(
                        [
                            'id_product' => $productId,
                            'id_variation' => $variationId,
                            'id_store' => $storeId,
                        ]
                    )->execute();

                return (int) sqlInsertId();
            });
        }

        return $id;
    }

    public function syncStore($value, $object): void
    {
        if ($id = ($object['id'] ?? false)) {
            $this->updateSQL(
                'stores_items',
                [
                    'quantity' => $value,
                ],
                ['id' => $id]
            );
        }
    }

    protected function postprocess()
    {
        // pokud mám$forceSyncID, tak neprovádím recalculateStores
        if (!empty($this->forceSyncID)) {
            return;
        }
        $this->helper->recalculateStores();

        \Variations::recalcInStore();
    }

    protected function getObjectID($item)
    {
        return [
            'id_store' => $this->helper->trimVarioId($item->Data->StoreID),
            'id_product' => $this->helper->trimVarioId($item->Data->ProductID),
            'id_variation' => $this->helper->trimVarioId($item->Data->VariantID),
        ];
    }

    protected function getSyncAllObjects()
    {
        return sqlQueryBuilder()->select('id_vario')
            ->from('vario_products', 'vp')
            ->join('vp', 'products', 'p', 'p.id = vp.id_product')
            ->andWhere('p.in_store <= 0 AND p.date_added >= \'2020-11-05\'')
//            ->andWhere('p.figure = "Y"')
            ->orderBy('id_vario');
    }
}
