<?php

namespace KupShop\WarehouseBundle\Tests;

use KupShop\WarehouseBundle\Util\InventoryWorker;
use KupShop\WarehouseBundle\Util\StoreItemWorker;
use Query\Operator;

class StoreAppInventoryTest extends \DatabaseTestCase
{
    /** @var StoreItemWorker */
    protected $storeItemWorker;

    /** @var InventoryWorker */
    protected $inventoryWorker;

    protected function setUp(): void
    {
        parent::setUp();

        $this->storeItemWorker = $this->get(StoreItemWorker::class);
        $this->inventoryWorker = $this->get(InventoryWorker::class);
    }

    public function testInventoryExistedProducts()
    {
        // store in items
        $products = $this->storeItemWorker->getProductsOnPositions([5]);
        $this->assertEquals(1, count($products));
        $this->assertEquals(1, $products[0]['id_product']);
        $this->assertEquals(2, $products[0]['pieces']);
        $this->assertEquals(2, sqlQueryBuilder()->select('in_store')->from('products')->where(Operator::equals(['id' => 1]))->execute()->fetchColumn());

        $this->inventoryWorker->submitInventory(5,
            $this->getItem(5, 4524667587750));

        $products = $this->storeItemWorker->getProductsOnPositions([5]);
        $this->assertEquals(1, count($products));
        $this->assertEquals(1, $products[0]['id_product']);
        $this->assertEquals(5, $products[0]['pieces']);
        $this->assertEquals(5, sqlQueryBuilder()->select('in_store')->from('products')->where(Operator::equals(['id' => 1]))->execute()->fetchColumn());
    }

    public function testInventoryCheckZeroProducts()
    {
        // store in items
        $products = $this->storeItemWorker->getProductsOnPositions([5]);
        $this->assertEquals(1, count($products));
        $this->assertEquals(1, $products[0]['id_product']);
        $this->assertEquals(2, $products[0]['pieces']);
        $this->assertEquals(2, sqlQueryBuilder()->select('in_store')->from('products')->where(Operator::equals(['id' => 1]))->execute()->fetchColumn());

        $this->inventoryWorker->submitInventory(5,
            $this->getItem(0, 4524667587750));

        $products = $this->storeItemWorker->getProductsOnPositions([5]);
        $this->assertEquals(0, count($products));
        $this->assertEquals(0, sqlQueryBuilder()->select('in_store')->from('products')->where(Operator::equals(['id' => 1]))->execute()->fetchColumn());
    }

    public function testInventoryNewProductOnThisPosition()
    {
        // store in items
        $products = $this->storeItemWorker->getProductsOnPositions([5]);
        $this->assertEquals(1, count($products));
        $this->assertEquals(0, sqlQueryBuilder()->select('in_store')->from('products')->where(Operator::equals(['id' => 2]))->execute()->fetchColumn());

        $this->inventoryWorker->submitInventory(5,
            $this->getItem(5, 4524667587751)
        );

        $products = $this->storeItemWorker->getProductsOnPositions([5]);
        $this->assertEquals(2, count($products));
        $this->assertEquals(2, $products[0]['id_product']);
        $this->assertEquals(5, $products[0]['pieces']);
        $this->assertEquals(5, sqlQueryBuilder()->select('in_store')->from('products')->where(Operator::equals(['id' => 2]))->execute()->fetchColumn());
    }

    public function testInventoryLogChange()
    {
        $this->inventoryWorker->submitInventory(5,
            $this->getItem(5, 4524667587750));

        $logRow = sqlQueryBuilder()
            ->select('*')
            ->from('warehouse_log')
            ->orderBy('id', 'DESC')
            ->setMaxResults(1)
            ->execute()->fetch();
        $this->assertEquals(1, $logRow['id_product']);
        $this->assertEquals(3, $logRow['pieces']);
        $this->assertEquals($this->inventoryWorker->getInventoryPosition(), $logRow['old_id_position']);
        $this->assertEquals(5, $logRow['new_id_position']);
        $this->assertEquals(null, $logRow['note']);
    }

    public function testSavingLastInventoryDate()
    {
        $this->inventoryWorker->submitInventory(5,
            $this->getItem(5, 4524667587750));

        $this->assertNotNull(sqlQueryBuilder()
            ->select('inventory_date')
            ->from('warehouse_products')
            ->where(Operator::equalsNullable(['id_product' => 1, 'id_variation' => null, 'id_position' => 5]))
            ->execute()->fetchColumn());

        $this->assertNotNull(sqlQueryBuilder()
            ->select('inventory_date')
            ->from('warehouse_positions')
            ->where(Operator::equalsNullable(['id' => 5]))
            ->execute()->fetchColumn());
    }

    protected function getItem($pcs, $ean)
    {
        $item = new \stdClass();
        $item->ean = $ean;
        $item->checked = $pcs;
        $item->over_supply = false;

        return [
            $item,
        ];
    }

    protected function getDataSet()
    {
        return $this->getJsonDataSetFromFile();
    }
}
