<?php

namespace KupShop\StoresBundle\Tests;

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

class StoresWarehouseFixErrorsTest extends \DatabaseTestCase
{
    /**
     * @var StoreWarehouseRecalculate
     */
    protected $storeWarehouseRecalculate;

    protected function setUp(): void
    {
        parent::setUp();
        $this->storeWarehouseRecalculate = $this->get(StoreWarehouseRecalculate::class);
    }

    public function testFixStoreWarehouseErrors()
    {
        // Kontrola, že není ve storu PO ani hlavní sklad
        $storeItems = sqlQueryBuilder()
            ->select('quantity')
            ->from('stores_items')
            ->where(Operator::equals(['id_product' => '747', 'id_variation' => '211935']))
            ->orWhere(Operator::equals(['id_store' => 10]))
            ->orWhere(Operator::equals(['id_store' => 1]))
            ->execute()
            ->fetchAllAssociative();
        $this->assertEquals(0, count($storeItems));

        // Kontrola počtu kusů ve warehouse které nejsou přiřazeny přes lokace (tedy hlavní sklad)
        $warehouseItems = sqlQueryBuilder()
            ->select('pieces')
            ->from('warehouse_products', 'wp')
            ->leftJoin('wp', 'warehouse_positions', 'wpos', 'wp.id_position = wpos.id')
            ->where(
                Operator::andX(
                    Operator::equals(['wp.id_product' => '747', 'wp.id_variation' => '211935']),
                    Operator::not(Operator::equals(['wp.id_position' => '3243'])), // id_base_position z PO
                    Operator::not(Operator::inStringArray(['45', '46'], 'wpos.id_location')) // lokace z PO
                )
            )
            ->execute()
            ->fetchAllAssociative();
        $this->assertEquals(2, $warehouseItems[0]['pieces']); // Očekávám 2 kusy na hlavní sklad

        // Kontrola počtu kusů ve warehouse které jsou přiřazeny k PO
        $warehouseItems = sqlQueryBuilder()
            ->select('pieces')
            ->from('warehouse_products', 'wp')
            ->leftJoin('wp', 'warehouse_positions', 'wpos', 'wp.id_position = wpos.id')
            ->where(
                Operator::andX(
                    Operator::equals(['wp.id_product' => '747', 'wp.id_variation' => '211935']),
                    Operator::orX(
                        Operator::equals(['wp.id_position' => '3243']), // id_base_position z PO
                        Operator::inStringArray(['45', '46'], 'wpos.id_location') // lokace z PO
                    )
                )
            )
            ->execute()
            ->fetchAllAssociative();
        $this->assertEquals(4, $warehouseItems[0]['pieces']); // Očekávám 4 kusy na hlavní sklad

        $this->storeWarehouseRecalculate->fixStoreWarehouseErrors(false, true);

        // Kontrola počtu kusů přidaných do store items | Na pozici STUL-VÝSTAVA byly 4 kusy -> na store PO(id: 10)
        $storeItems = sqlQueryBuilder()
            ->select('quantity, custom_data')
            ->from('stores_items')
            ->where(Operator::equals(['id_product' => '747', 'id_variation' => '211935', 'id_store' => 10]))
            ->execute()
            ->fetchAllAssociative();
        $this->assertEquals(1, count($storeItems));
        $this->assertEquals(4, $storeItems[0]['quantity']);
        $this->assertEmpty($storeItems[0]['custom_data']); // Kontrola, že se nenasetovaly custom data

        // Kontrola počtu kusů přidaných do store items | Na pozici POKUSNA-POZICE byly 2 kusy -> na hlavní sklad(id: 1)
        $storeItems = sqlQueryBuilder()
            ->select('quantity, custom_data')
            ->from('stores_items')
            ->where(Operator::equals(['id_product' => '747', 'id_variation' => '211935', 'id_store' => 1]))
            ->execute()
            ->fetchAllAssociative();
        $this->assertEquals(1, count($storeItems));
        $this->assertEquals(2, $storeItems[0]['quantity']);
        $this->assertEmpty($storeItems[0]['custom_data']); // Kontrola, že se nenasetovaly custom data
    }

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