<?php

declare(strict_types=1);

namespace External\PompoBundle\Resources\script;

use External\PompoBundle\DataGo\Synchronizer\OrderSynchronizer;
use External\PompoBundle\Util\PompoUtil;
use KupShop\AdminBundle\Util\Script\Script;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

class DataGoSynchronizationScript extends Script
{
    protected static $name = '[DataGo] Run synchronization';
    protected static $defaultParameters = [
        'type' => null,
        'ordersFromDataGo' => false,
        'forceProcess' => false,
    ];

    protected function run(array $arguments)
    {
        if ($arguments['ordersFromDataGo'] ?? false) {
            $this->log('Processing orders from DataGo...');
            $orderSynchronizer = ServiceContainer::getService(OrderSynchronizer::class);
            $orderSynchronizer->processOrdersFromDataGo();
            $this->log('OK. Processed.');

            return;
        }

        if (!($arguments['type'] ?? false)) {
            $this->log('You must specify type!');

            return;
        }

        $pompoUtil = ServiceContainer::getService(PompoUtil::class);

        $this->log('Running...');

        $pompoUtil->synchronizeDataGo([$arguments['type']], $arguments['forceProcess'] ?? false);

        $this->log('Done');
    }
}

return DataGoSynchronizationScript::class;
