<?php

declare(strict_types=1);

namespace External\FlexiBeeBundle\EventSubscriber;

use External\FlexiBeeBundle\Util\FlexiBeeUtil;
use KupShop\OrderingBundle\Event\OrderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OrderSubscriber implements EventSubscriberInterface
{
    private FlexiBeeUtil $flexiBeeUtil;

    public function __construct(FlexiBeeUtil $flexiBeeUtil)
    {
        $this->flexiBeeUtil = $flexiBeeUtil;
    }

    public static function getSubscribedEvents(): array
    {
        return [
            OrderEvent::ORDER_PAID => [
                ['updateFlexiBeeOrder', 200],
            ],
            OrderEvent::ORDER_EDITED => [
                ['updateFlexiBeeOrder', 200],
            ],
            OrderEvent::ORDER_STORNO => [
                ['updateFlexiBeeOrder', 200],
            ],
        ];
    }

    public function updateFlexiBeeOrder(OrderEvent $event): void
    {
        $order = $event->getOrder();

        // synchronizace na zaklade toho znovu odesle objednavku do FlexiBee - aktualizuje ji
        $this->flexiBeeUtil->setFlexiOrderData(
            (int) $order->id,
            'orderUpdated',
            1
        );
    }
}
