<?php

declare(strict_types=1);

namespace KupShop\PreordersBundle\Tests\Services;

use KupShop\DevelopmentBundle\Util\Tests\CartTestTrait;
use KupShop\KupShopBundle\Context\PriceLevelContext;
use KupShop\PreordersBundle\Entity\UserPreorder;
use KupShop\PreordersBundle\Service\DynamicContexts;
use KupShop\PreordersBundle\Tests\PreordersTestHelper;
use KupShop\PricelistBundle\Context\PricelistContext;

class ContextsTest extends \DatabaseTestCase
{
    use PreordersTestHelper;
    use CartTestTrait;

    private DynamicContexts $contextsService;

    protected function setUp(): void
    {
        parent::setUp();

        $this->contextsService = $this->get(DynamicContexts::class);
    }

    public function testBaseContexts()
    {
        $userPreorder = $this->createTestUserPreorder();

        $this->contextsService->withBasePrices($userPreorder->getPreorder(), function () {
            $this->assertEquals(
                self::PRICE_LEVEL_BASE,
                (int) $this->get(PriceLevelContext::class)->getActiveId(),
            );

            $this->assertEquals(
                self::PRICELIST_BASE,
                (int) $this->get(PricelistContext::class)->getActiveId(),
            );
        });
    }

    public function testDynamicContexts()
    {
        $this->loginUser(1);
        $dateId = $this->createDate(1);

        $userPreorder = new UserPreorder(1, $dateId);

        // 4132 Kč
        $userPreorder->setItem(
            idProduct: 4,
            idVariation: 1,
            count: 1,
        );

        // 4132 Kč
        $userPreorder->setItem(
            idProduct: 4,
            idVariation: 2,
            count: 1,
        );

        $this->contextsService->withDynamicPrices(
            $userPreorder->getPreorder(),
            $userPreorder->getItems(true),
            function () {
                $this->assertEquals(
                    self::PRICE_LEVEL_8,
                    (int) $this->get(PriceLevelContext::class)->getActiveId(),
                    'Neaktivovala se očekávaná slevová hladina.',
                );

                $this->assertEquals(
                    self::PRICELIST_BASE,
                    (int) $this->get(PricelistContext::class)->getActiveId(),
                    'Neaktivovala se očekávaná slevová hladina.',
                );
            },
        );

        // 1748 Kč
        $userPreorder->setItem(
            idProduct: 10,
            count: 3,
        );

        $this->contextsService->withDynamicPrices(
            $userPreorder->getPreorder(),
            $userPreorder->getItems(true),
            function () {
                $this->assertEquals(
                    self::PRICE_LEVEL_12,
                    (int) $this->get(PriceLevelContext::class)->getActiveId(),
                    'Neaktivovala se očekávaná slevová hladina.',
                );

                $this->assertEquals(
                    self::PRICELIST_BASE,
                    (int) $this->get(PricelistContext::class)->getActiveId(),
                    'Neaktivovala se očekávaná slevová hladina.',
                );
            },
        );

        // 1748 Kč
        $userPreorder->setItem(
            idProduct: 10,
            count: 20,
        );

        $this->contextsService->withDynamicPrices(
            $userPreorder->getPreorder(),
            $userPreorder->getItems(true),
            function () {
                $this->assertEquals(
                    self::PRICE_LEVEL_16,
                    (int) $this->get(PriceLevelContext::class)->getActiveId(),
                    'Neaktivovala se očekávaná slevová hladina.',
                );

                $this->assertEquals(
                    self::PRICELIST_INF,
                    (int) $this->get(PricelistContext::class)->getActiveId(),
                    'Neaktivovala se očekávaná slevová hladina.',
                );
            },
        );
    }
}
