<?php

declare(strict_types=1);

namespace External\PompoBundle\Ordering\Triggers;

use KupShop\AdminBundle\Util\LegacyAdminCredentials;
use KupShop\OrderDiscountBundle\Triggers\AbstractTrigger;
use KupShop\OrderingBundle\Entity\Purchase\PurchaseState;

class POSTrigger extends AbstractTrigger
{
    /** @required */
    public LegacyAdminCredentials $legacyAdminCredentials;

    protected static $type = 'pompo_pos';
    protected $adminTemplate = 'triggers/pompo_pos.tpl';

    public function getName()
    {
        return '[PompoAPI] Lze uplatnit na pokladnách';
    }

    public function getDescription()
    {
        return 'Lze uplatnit na pokladně';
    }

    public function getDiscountFilterSpec(PurchaseState $purchaseState)
    {
        // pokud je to API volani z pokladny
        if ($this->isPompoApi($purchaseState)) {
            // vyhodim vsechny slevy, ktere se nedaji uplatnit na pokladne
            return '(SELECT odt_pompo.type FROM order_discounts_triggers odt_pompo WHERE odt_pompo.type = "'.static::getType().'" AND odt_pompo.id_order_discount = odt.id_order_discount) IS NULL';
        }

        return null;
    }

    private function isPompoApi(PurchaseState $purchaseState): bool
    {
        if ($purchaseState->getCustomData('isPompoApi')) {
            return true;
        }

        return false;
    }
}
