<?php

namespace KupShop\StoresBundle\Tests;

use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;
use KupShop\StoresBundle\Utils\MainStoreMigrationUtil;
use KupShop\StoresBundle\Utils\StoresInStore;

class StoresOrdersTest extends \DatabaseTestCase
{
    use CartTestTrait;

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

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

    public function testUpdateStoreDeliveryChanged()
    {
        $this->prepareCart(); // delivery = ČESKÁ POŠTA - BALÍK DO RUKY
        $this->insertProduct(1, null, 3); // 3 ks
        $product = new \Product(1);
        $product->createFromDB(1);
        self::assertEquals(5, $product->inStore); // 5 ks = 2 ks + 3 ks

        $inStore = [
            'total_in_store' => 5,
            'store2_in_store' => 3,     // Prodejna
            'store2_min_in_store' => 0,
            'store1_in_store' => 2,     // Hlavni sklad
            'store1_min_in_store' => 0,
        ];

        $productInStoreAmounts = $this->storesInStore->getProductInStoreAmounts(1, null);
        self::assertEquals($inStore, $productInStoreAmounts);

        $order = $this->getOrder();
        $product->createFromDB(1);
        self::assertEquals(2, $product->inStore);

        $inStore = [
            'total_in_store' => 2,
            'store2_in_store' => 3,      // Prodejna
            'store2_min_in_store' => 0,
            'store1_in_store' => -1,     // Hlavni sklad
            'store1_min_in_store' => 0,
        ];
        $productInStoreAmounts = $this->storesInStore->getProductInStoreAmounts(1, null);
        self::assertEquals($inStore, $productInStoreAmounts);

        $order->changeDeliveryType(2);  // delivery = OSOBNÍ ODBĚR - PRODEJNA
        $inStore = [
            'total_in_store' => 2,
            'store2_in_store' => 0,      // Prodejna
            'store2_min_in_store' => 0,
            'store1_in_store' => 2,      // Hlavni sklad
            'store1_min_in_store' => 0,
        ];
        $productInStoreAmounts = $this->storesInStore->getProductInStoreAmounts(1, null);
        self::assertEquals($inStore, $productInStoreAmounts);

        //        $order->storno();
        //        $inStore = [
        //            'total_in_store' => 5,
        //            'store2_in_store' => 3,      // Prodejna
        //            'store2_min_in_store' => 0,
        //            'store1_in_store' => 2,      // Hlavni sklad
        //            'store1_min_in_store' => 0,
        //        ];
        //        $productInStoreAmounts = $this->storesInStore->getProductInStoreAmounts(1, null);
        //        self::assertEquals($inStore, $productInStoreAmounts);
    }

    public function getOrder()
    {
        $this->visitStep('delivery');
        $this->cart->createFromDB();

        /** @var \Order $order */
        $order = $this->cart->submitOrder();
        $this->assertInstanceOf(\Order::class, $order);

        return $order;
    }

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