<?php

namespace KupShop\AutomationConfiguratorBundle\Configurator\Orders\Conditions\Fields;

use KupShop\AutomationConfiguratorBundle\Configurator\Interfaces\Conditions\Fields\Field;
use KupShop\AutomationConfiguratorBundle\Configurator\Orders\OrdersContext;
use KupShop\AutomationConfiguratorBundle\Util\ExpressionContext;

class HasPackageNo extends Field implements OrdersContext
{
    public function getConfig(): array
    {
        return [
            'label' => 'Má číslo balíku',
            'type' => 'boolean',
        ];
    }

    public function getGroupName(): ?string
    {
        return 'order_flags';
    }

    public function getName(): string
    {
        return 'package_id';
    }

    public function getCompareValue(ExpressionContext $context, $args = null)
    {
        $context = $context->getContext();

        return !empty($context['o_package_id']);
    }

    public function modifyPrefetchFastCondition($context, $jsonValue)
    {
        $value = $this->prefetchUtil->getBooleanValue($jsonValue);

        if ($value) {
            $context->andWhere('o.package_id IS NOT NULL');
        } else {
            $context->andWhere('o.package_id IS NULL');
        }

        return $context;
    }
}
