<?php

declare(strict_types=1);

namespace KupShop\StoresBundle\Tests;

use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;
use KupShop\OrderingBundle\Event\OrderEvent;
use KupShop\StoresBundle\Utils\StoresInStore;

class SellersStoresOrdersTest extends \DatabaseTestCase
{
    use CartTestTrait;

    protected StoresInStore $storesInStore;
    protected $dispatcher;

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

    public function setUp(): void
    {
        parent::setUp();
        $this->storesInStore = $this->get(StoresInStore::class);
        $this->dispatcher = $this->get('event_dispatcher');
    }

    public function testChangeSeller(): void
    {
        $in_store_1 = $this->storesInStore->getProductInStores(23104, null);

        $expected_1 = [
            1 => 0,
            2 => 1,
            5 => 1,
            6 => 1,
            7 => 1,
            8 => 1,
            9 => 0,
            10 => 1,
            11 => 5,
        ];

        $this->assertEquals($expected_1, $in_store_1);

        $this->prepareCart();
        $this->setDeliveryType(10);
        $this->insertProduct(23104, null, 2);

        $this->cart->createFromDB();
        $this->cart->deliveryData['seller_id'] = 7;
        $order = $this->cart->submitOrder();

        $in_store_2 = $this->storesInStore->getProductInStores(23104, null);

        $expected_2 = [
            1 => 0,
            2 => 1,
            5 => 1,
            6 => 1,
            7 => 1,
            8 => 1,
            9 => 0,
            10 => -1,
            11 => 5,
        ];
        $this->assertEquals($expected_2, $in_store_2);
        $delivery_data = $order->getData('delivery_data');
        $order->setData('delivery_data_old', $delivery_data);
        $delivery_data['seller_id'] = 2;
        $order->setData('delivery_data', $delivery_data);
        $event = new OrderEvent($order);
        $this->dispatcher->dispatch($event, OrderEvent::ORDER_DELIVERY_CHANGED);

        $in_store_3 = $this->storesInStore->getProductInStores(23104, null);

        $expected_3 = [
            1 => 0,
            2 => -1,
            5 => 1,
            6 => 1,
            7 => 1,
            8 => 1,
            9 => 0,
            10 => 1,
            11 => 5,
        ];

        $this->assertEquals($expected_3, $in_store_3);
    }
}
