<?php

namespace External\FlexiBeeBundle\Admin\Actions;

use External\FlexiBeeBundle\Synchronizers\OrderSynchronizer;
use External\FlexiBeeBundle\Util\FlexiBeeUtil;
use KupShop\AdminBundle\Admin\Actions\AbstractAction;
use KupShop\AdminBundle\Admin\Actions\ActionResult;
use KupShop\AdminBundle\Admin\Actions\IAction;
use Symfony\Contracts\Service\Attribute\Required;

class OrderSendToFlexiAction extends AbstractAction implements IAction
{
    #[Required]
    public OrderSynchronizer $orderSynchronizer;

    #[Required]
    public FlexiBeeUtil $flexiUtil;

    public function getTypes(): array
    {
        return ['orders'];
    }

    public function getName(): string
    {
        return '[Flexi] Odeslat do Flexi';
    }

    public function isVisible()
    {
        return !empty($this->getId());
    }

    public function showInMassEdit()
    {
        return true;
    }

    public function execute(&$data, array $config, string $type): ActionResult
    {
        $orderId = (int) $this->getId();

        if ($this->orderSynchronizer->syncSingleItemToFlexi([
            'id' => $orderId,
            'id_flexi' => $this->flexiUtil->getMapping('order', $orderId),
        ])) {
            return new ActionResult(true);
        }

        return new ActionResult(false, 'Odeslání do Flexi se nezdařilo.');
    }
}
