<?php

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

use KupShop\AutomationConfiguratorBundle\Configurator\Interfaces\Conditions\Fields\Field;
use KupShop\AutomationConfiguratorBundle\Configurator\Users\Conditions\Groups\UserFlags;
use KupShop\AutomationConfiguratorBundle\Configurator\Users\UsersContext;
use KupShop\AutomationConfiguratorBundle\Util\ExpressionContext;

class UserIsHasDIC extends Field implements UsersContext
{
    public function getConfig(): array
    {
        return [
            'label' => 'Má vyplněné DIC',
            'type' => 'boolean',
        ];
    }

    public function getGroupName(): ?string
    {
        return UserFlags::NAME;
    }

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

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

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

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

        if ($value) {
            $context->andWhere("u.dic != ''");
        } else {
            $context->andWhere("u.dic = ''");
        }

        return $context;
    }
}
