<?php

declare(strict_types=1);

namespace External\ZNZBundle\Context;

use External\ZNZBundle\Util\ZNZConfiguration;
use KupShop\KupShopBundle\Context\PriceLevelContext;
use KupShop\KupShopBundle\Util\Contexts;
use KupShop\PricelistBundle\Context\PricelistContext;
use Symfony\Contracts\Service\Attribute\Required;

class CacheContext extends \KupShop\KupShopBundle\Context\CacheContext
{
    #[Required]
    public ZNZConfiguration $configuration;

    protected function getKeyFullPage(): ?array
    {
        return array_merge($this->getKeyText(), $this->getKeyPrice(), $this->getJsShopSectionKey(), $this->getKeyAvailability());
    }

    protected function getKeyAvailability(): ?array
    {
        if (!$this->configuration->isB2BShop()) {
            return [];
        }

        return [
            \KupShop\KupShopBundle\Context\UserContext::class => Contexts::get(\KupShop\KupShopBundle\Context\UserContext::class)->getActiveId(),
        ];
    }

    protected function getKeyDelivery(): ?array
    {
        $languageContext = Contexts::get(\KupShop\KupShopBundle\Context\LanguageContext::class);
        $countryContext = Contexts::get(\KupShop\KupShopBundle\Context\CountryContext::class);

        return [
            \KupShop\KupShopBundle\Context\LanguageContext::class => $languageContext->getActiveId(),
            \KupShop\KupShopBundle\Context\CountryContext::class => $countryContext->getActiveId(),
        ];
    }

    protected function getUserPriceLevelKey(): array
    {
        $priceLevelContext = Contexts::get(PriceLevelContext::class);

        return [PriceLevelContext::class => $priceLevelContext->getActiveId()];
    }

    protected function getKeyPrice(): ?array
    {
        return array_merge(
            parent::getKeyPrice(),
            $this->getUserPriceLevelKey(),
            [PricelistContext::class => Contexts::get(PricelistContext::class)->getActiveId()]
        );
    }

    protected function getDefaultKeyContexts(): array
    {
        return array_merge(parent::getDefaultKeyContexts(), [
            PriceLevelContext::class => \KupShop\KupShopBundle\Context\ContextManager::FORCE_EMPTY,
        ]);
    }
}
