<?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 PendingNote extends Field implements OrdersContext
{
    public function getConfig(): array
    {
        return [
            'label' => 'Má vyřízenou poznámku',
            'type' => 'boolean',
            'tooltip' => 'Objednávka má vyřízenou poznámku od zákazníka',
        ];
    }

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

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

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

        return !in_array('R', explode(',', $context['o_flags']));
    }

    public function modifyPrefetchFastCondition($context, $jsonValue)
    {
        $key = array_key_first($jsonValue);

        if (!empty($key)) {
            $value = $jsonValue[$key][1] ?? null;

            if (is_bool($value)) {
                if ($value) {
                    $context->andWhere(\Query\Operator::not('FIND_IN_SET("R",o.flags)'));
                } else {
                    $context->andWhere('FIND_IN_SET("R", o.flags)');
                }
            }
        }

        return $context;
    }
}
