<?php

namespace KupShop\StoresBundle\Tests;

use KupShop\StoresBundle\Utils\StoreTransferCheckoutWorker;
use Query\Operator;

class StoresTransferCheckoutTest extends \DatabaseTestCase
{
    protected function setUp(): void
    {
        parent::setUp();

        $this->worker = $this->get(StoreTransferCheckoutWorker::class);
    }

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

    public function testTransferCheckoutWorkerOnEmpty()
    {
        // Empty check returns error
        $checkout = $this->worker->checkoutStoreTransfer(1, []);
        $this->assertFalse($checkout['result']);
        $this->assertEquals($checkout['message'], 'Nebyla zkontrolována žádná položka!');
    }

    public function testTransferCheckoutWorkerIncomplete()
    {
        // Incomplete check modifies transfer
        $items = [
            [
                'id' => 10,
                'checked' => 2,
            ],
            [
                'id' => 20,
                'checked' => 1,
            ],
        ];
        $this->assertEquals($this->getProductInStore(2, 1), 3);
        $this->assertEquals($this->getProductInStore(2, 2), 3);
        $checkout = $this->worker->checkoutStoreTransfer(1, json_decode(json_encode($items)));
        $this->assertEquals($this->getProductInStore(2, 1), 5);
        $this->assertEquals($this->getProductInStore(2, 2), 4);
        $this->assertTrue($checkout['result']);
    }

    public function testTransferCheckoutWorkerWithInFlightStoreIncomplete()
    {
        // Incomplete check with in-flight-store returns error
        $this->addModule(\Modules::STORES, \Modules::SUB_ON_THE_FLY_STORE);
        $items = [
            [
                'id' => 10,
                'checked' => 2,
            ],
            [
                'id' => 20,
                'checked' => 1,
            ],
        ];
        $checkout = $this->worker->checkoutStoreTransfer(1, json_decode(json_encode($items)));
        $this->assertFalse($checkout['result']);
        $this->assertEquals($checkout['message'], 'Nebyly zkontrolovány všechny položky. Není možné příjmout převodku z meziskladu.');
    }

    public function testTransferCheckoutWorkerWithInFlightStoreComplete()
    {
        // Complete check with in-flight-store is OK
        $this->addModule(\Modules::STORES, \Modules::SUB_ON_THE_FLY_STORE);
        $items = [
            [
                'id' => 10,
                'checked' => 4,
            ],
            [
                'id' => 20,
                'checked' => 1,
            ],
        ];
        $this->assertEquals($this->getProductInStore(2, 1), 3);
        $this->assertEquals($this->getProductInStore(2, 2), 3);
        $checkout = $this->worker->checkoutStoreTransfer(1, json_decode(json_encode($items)));
        $this->assertEquals($this->getProductInStore(2, 1), 7);
        $this->assertEquals($this->getProductInStore(2, 2), 4);
        $this->assertTrue($checkout['result']);
    }

    protected function getProductInStore($id_store, $id_product)
    {
        return sqlQueryBuilder()->select('quantity')
            ->from('stores_items')
            ->where(Operator::equals(['id_store' => $id_store, 'id_product' => $id_product]))
            ->execute()
            ->fetchOne();
    }

    protected function getTransferQuantity($id_transfer, $id_product)
    {
        return sqlQueryBuilder()->select('quantity')
            ->from('stores_transfers')
            ->where(Operator::equals(['id_transfer' => $id_transfer, 'id_product' => $id_product]))
            ->execute()
            ->fetchOne();
    }
}
