<?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 UserIsActive extends Field implements UsersContext
{
    public function getConfig(): array
    {
        return [
            'label' => 'Je aktivní',
            'type' => 'boolean',
        ];
    }

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

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

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

        return $context['u_figure'] == 'Y';
    }

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

        if ($value) {
            $context->andWhere("u.figure = 'Y'");
        } else {
            $context->andWhere("u.figure = 'N'");
        }

        return $context;
    }
}
