<?php

namespace KupShop\StoresBundle\Tests;

use KupShop\StoresBundle\Utils\MainStoreMigrationUtil;
use KupShop\StoresBundle\Utils\StoresInStore;
use KupShop\StoresBundle\Utils\StoresTransfers;

class StoresMissingTransferTest extends \DatabaseTestCase
{
    /**
     * @var StoresTransfers
     */
    protected $storesTransfers;

    /** @var StoresInStore */
    protected $storesInStore;

    protected function setUp(): void
    {
        parent::setUp();
        $this->storesTransfers = $this->get(StoresTransfers::class);
        $this->storesInStore = $this->get(StoresInStore::class);
        $this->removeModule(\Modules::WAREHOUSE);
        /** @var MainStoreMigrationUtil $migrate */
        $migrate = $this->get(MainStoreMigrationUtil::class);
        $migrate->migrate();
    }

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

    public function testTransferEmpty()
    {
        $missingItems = $this->storesTransfers->getMissingItems(1, 2);
        self::assertEquals([], $missingItems);
        $missingItems = $this->storesTransfers->getMissingItems(2, 1);
        self::assertEquals([], $missingItems);
    }

    // Chybí mi jeden kus (objednaný) a zároveň hcci mít 3 v zásobě, proto převedu 4
    public function testStoreMissingItem()
    {
        $this->insertSQL('stores_items', [
            'id_store' => 2,
            'id_product' => 3,
            'id_variation' => 1,
            'quantity' => -1,
            'min_quantity' => 3,
        ]);

        $missingItems = $this->storesTransfers->getMissingItems(1, 2);

        self::assertEquals(1, count($missingItems));
        $missingItem = reset($missingItems);
        self::assertEquals(3, $missingItem['id_product']);
        self::assertEquals(1, $missingItem['id_variation']);
        self::assertEquals(4, $missingItem['quantity']);
    }

    // Chybí mi jeden kus (objednaný) a zároveň chci mít 4 v rezervě, ale 1 už je na cestě. Tedy převedu (1 + 4 - 1) = 4
    public function testStoreMissingItemTransfer()
    {
        $this->insertSQL('stores_items', [
            'id_store' => 2,
            'id_product' => 3,
            'id_variation' => 1,
            'quantity' => -1,
            'min_quantity' => 4,
        ]);

        $this->insertSQL('stores_transfers_items', [
            'id_stores_transfer' => 1,
            'id_product' => 3,
            'id_variation' => 1,
            'quantity' => 1,
        ]);

        $missingItems = $this->storesTransfers->getMissingItems(1, 2);

        self::assertEquals(1, count($missingItems));
        $missingItem = reset($missingItems);
        self::assertEquals(3, $missingItem['id_product']);
        self::assertEquals(1, $missingItem['id_variation']);
        self::assertEquals(4, $missingItem['quantity']);
    }

    // Mám situaci jako předchozí, ale nemůžu převést tolik kolik bych chtěl, protože už existuje jiná otevřená převodka,
    // která ty kusy rezervuje. 1ks objednávka + 4 ks min quantity = 5, jeden už je ale na cestě, takže jen 4
    // jeden je ale na cestě do jinýho skladu, takže mám k dispozici jen 3
    public function testStoreMissingItemTransfer2()
    {
        $this->insertSQL('stores_items', [
            'id_store' => 2,
            'id_product' => 3,
            'id_variation' => 1,
            'quantity' => -1,
            'min_quantity' => 4,
        ]);

        $this->insertSQL('stores_transfers_items', [
            'id_stores_transfer' => 1,
            'id_product' => 3,
            'id_variation' => 1,
            'quantity' => 1,
        ]);

        $this->insertSQL('stores_transfers_items', [
            'id_stores_transfer' => 3,
            'id_product' => 3,
            'id_variation' => 1,
            'quantity' => 1,
        ]);

        $missingItems = $this->storesTransfers->getMissingItems(1, 2);

        self::assertEquals(1, count($missingItems));
        $missingItem = reset($missingItems);
        self::assertEquals(3, $missingItem['id_product']);
        self::assertEquals(1, $missingItem['id_variation']);
        self::assertEquals(3, $missingItem['quantity']);
    }

    public function testMainStoreMissingItem()
    {
        // Nastav celkovou skladovost na 2, si1 = -1, si2 = 3
        $this->storesInStore->updateStoreItemWithRecalculate(['id_store' => 1, 'quantity' => -1, 'id_product' => 1], false);

        $missingItems = $this->storesTransfers->getMissingItems(2, 1);

        self::assertEquals(1, count($missingItems));
        $missingItem = reset($missingItems);
        self::assertEquals(1, $missingItem['id_product']);
        self::assertEquals(1, $missingItem['quantity']);
    }
}
