<?php

namespace KupShop\AutomationConfiguratorBundle\Configurator\Users\Actions;

use KupShop\AutomationConfiguratorBundle\Configurator\Interfaces\Config\ActionConfig;
use KupShop\AutomationConfiguratorBundle\Event\TriggerEvent;
use Query\Operator;

class RemoveUserGroup extends UserGroupAction
{
    public const NAME = 'remove_user_group';

    public const ACTION_LABEL = 'Odebrat uživatelskou skupinu';

    public function run(ActionConfig $actionConfig, $context, TriggerEvent $triggerEvent): bool
    {
        $group = $this->getSelectedGroup($actionConfig);

        if (!$group) {
            return false;
        }

        $res = sqlQueryBuilder()->delete('users_groups_relations')
            ->where(Operator::equals([
                'id_group' => $group->getValue(),
                'id_user' => $context['id'],
            ]))->execute();

        return (bool) $res;
    }

    public function getLog(ActionConfig $actionConfig, $context)
    {
        $group = $this->getSelectedGroup($actionConfig);

        if (!$group) {
            return false;
        }

        return "Uživateli {$context['u_log_info']} byla odebrána skupina \"{$this->getGroupTitle($group->getValue())}\"";
    }
}
