<?php

namespace KupShop\AutomationConfiguratorBundle\EventListener;

use KupShop\AutomationConfiguratorBundle\Configurator\Orders\Triggers\OrderCreated;
use KupShop\AutomationConfiguratorBundle\Configurator\Orders\Triggers\OrderPaid;
use KupShop\AutomationConfiguratorBundle\Configurator\Orders\Triggers\OrderStorno;
use KupShop\AutomationConfiguratorBundle\Configurator\Orders\Triggers\StatusChange;
use KupShop\AutomationConfiguratorBundle\Event\TriggerEvent;
use KupShop\AutomationConfiguratorBundle\Util\ConfigurationsUtil;
use KupShop\AutomationConfiguratorBundle\Util\PostOrderFinishedRunner;
use KupShop\OrderingBundle\Event\OrderEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class OrdersTriggersSubscriber implements EventSubscriberInterface
{
    public function __construct(
        private readonly ConfigurationsUtil $configurationsUtil,
        protected readonly PostOrderFinishedRunner $postOrderFinishedRunner,
    ) {
    }

    public static function getSubscribedEvents(): array
    {
        // return the subscribed events, their methods and priorities
        return [
            OrderEvent::ORDER_STATUS_CHANGED => [
                ['statusChangeTrigger', 0],
            ],
            OrderEvent::ORDER_STORNO => [
                ['orderStornoTrigger', 0],
            ],
            OrderEvent::ORDER_PAID => [
                ['orderPaidTrigger', 0],
            ],
            // snížena priorita, kvůli DPH u objednávek: u zahraničních (DIČ) objednávek měl autopilot v době odeslání
            // e-mailu navěšenou cenu u dopravy s DPH
            OrderEvent::ORDER_COMPLETE => [
                ['orderCompletedTrigger', -200],
            ],
            OrderEvent::ORDER_FINISHED => [
                ['orderFinishedEvent', -200],
            ],
        ];
    }

    public function statusChangeTrigger(OrderEvent $event)
    {
        $order = $event->getOrder();

        // pokud je to zmena stavu v ramci stornovani objednavky, tak nechci triggerovat spoustec o zmene stavu
        if ($order->status_storno == 1) {
            return;
        }

        $this->configurationsUtil->runTrigger(new TriggerEvent(StatusChange::NAME, ['from_status' => (string) $order['status_previous'], 'to_status' => $order['status'], 'orderId' => $order->id, 'orderObject' => $order]));
    }

    public function orderStornoTrigger(OrderEvent $event): void
    {
        $this->configurationsUtil->runTrigger(new TriggerEvent(OrderStorno::NAME, ['orderObject' => $event->getOrder()]));
    }

    public function orderCompletedTrigger(OrderEvent $event)
    {
        $order = $event->getOrder();
        $this->configurationsUtil->runTrigger(new TriggerEvent(OrderCreated::NAME, ['orderObject' => $order]));
    }

    public function orderFinishedEvent(OrderEvent $event)
    {
        $this->postOrderFinishedRunner->executeCallbacks();
    }

    public function orderPaidTrigger(OrderEvent $event)
    {
        $order = $event->getOrder();
        $this->configurationsUtil->runTrigger(new TriggerEvent(OrderPaid::NAME, ['orderObject' => $order]));
    }
}
