<?php

declare(strict_types=1);

namespace External\PompoBundle\Resources\script;

use External\PompoBundle\DRS\Synchronizer\POSOrderSynchronizer;
use KupShop\AdminBundle\Util\Script\Script;
use KupShop\KupShopBundle\Util\Compat\ServiceContainer;

class ImportCouponsToPOSOrdersScript extends Script
{
    protected static $name = '[pompo] Import coupons to POS orders';

    protected function run(array $arguments)
    {
        $json = '[{"DocumentNumber":20109057,"DiscountNumber":"PK050CU7J1"},{"DocumentNumber":20109058,"DiscountNumber":"PK200K8WU1"},{"DocumentNumber":20109640,"DiscountNumber":"PK100W4NQ2"},{"DocumentNumber":20109775,"DiscountNumber":"NK010JZKF9"},{"DocumentNumber":20110315,"DiscountNumber":"PK1003URS2"},{"DocumentNumber":208202231,"DiscountNumber":"NK010189F3"},{"DocumentNumber":208202405,"DiscountNumber":"NK0101M79G"},{"DocumentNumber":20110576,"DiscountNumber":"NK010Q6J2D"},{"DocumentNumber":20110629,"DiscountNumber":"PK2009BUCZ"},{"DocumentNumber":20110641,"DiscountNumber":"PK05085ETR"},{"DocumentNumber":208202550,"DiscountNumber":"NK01056427"},{"DocumentNumber":208202627,"DiscountNumber":"NK0108M955"},{"DocumentNumber":208202656,"DiscountNumber":"NK0105K174"},{"DocumentNumber":208202660,"DiscountNumber":"NK010R2N96"},{"DocumentNumber":208202731,"DiscountNumber":"NK010Q3W1T"},{"DocumentNumber":208202769,"DiscountNumber":"NK0105T8G4"},{"DocumentNumber":208202716,"DiscountNumber":"NK010EZ85M"},{"DocumentNumber":208203076,"DiscountNumber":"NK010XL36V"},{"DocumentNumber":208203080,"DiscountNumber":"NK010P7Y4Z"},{"DocumentNumber":208203106,"DiscountNumber":"NK010H5D4N"},{"DocumentNumber":208203132,"DiscountNumber":"NK010D8SNC"},{"DocumentNumber":208203158,"DiscountNumber":"NK010FM668"},{"DocumentNumber":208203214,"DiscountNumber":"PK2503W6PS"},{"DocumentNumber":208203216,"DiscountNumber":"PK1008Z35J"},{"DocumentNumber":208203228,"DiscountNumber":"PK050684L7"},{"DocumentNumber":208203232,"DiscountNumber":"NK010KQB4C"},{"DocumentNumber":208203254,"DiscountNumber":"NK010G5RN3"},{"DocumentNumber":208203289,"DiscountNumber":"NK01051D27"},{"DocumentNumber":208203291,"DiscountNumber":"NK010A4XKQ"},{"DocumentNumber":20111353,"DiscountNumber":"NK010G9WY6"},{"DocumentNumber":20111386,"DiscountNumber":"NK01069U32"},{"DocumentNumber":208203390,"DiscountNumber":"PK250VUR43"},{"DocumentNumber":208203464,"DiscountNumber":"NK010V21QL"},{"DocumentNumber":20111474,"DiscountNumber":"PK100X7LRT"},{"DocumentNumber":208203503,"DiscountNumber":"PK100MPV37"},{"DocumentNumber":208203521,"DiscountNumber":"NK010HDYUN"},{"DocumentNumber":208203529,"DiscountNumber":"PK250WT48H"},{"DocumentNumber":208203529,"DiscountNumber":"PK100Z1EJW"},{"DocumentNumber":208203533,"DiscountNumber":"NK0102F2EQ"},{"DocumentNumber":208203537,"DiscountNumber":"PK500T5ZL6"},{"DocumentNumber":208203537,"DiscountNumber":"PK250A7URT"},{"DocumentNumber":208203555,"DiscountNumber":"NK0105ZNT2"},{"DocumentNumber":208203557,"DiscountNumber":"NK010TC3NW"},{"DocumentNumber":208203590,"DiscountNumber":"PK2501GNS3"},{"DocumentNumber":208203677,"DiscountNumber":"PK200BJ55F"},{"DocumentNumber":208203686,"DiscountNumber":"PK250Q9917"},{"DocumentNumber":20111677,"DiscountNumber":"NK01081XBM"}]';

        $synchronizer = ServiceContainer::getService(POSOrderSynchronizer::class);

        $data = json_decode($json, true);
        foreach ($data as $item) {
            $orderId = sqlQueryBuilder()
                ->select('do.id_order')
                ->from('drs_orders', 'do')
                ->join('do', 'orders', 'o', 'o.id = do.id_order')
                ->andWhere(\Query\Operator::equals(['JSON_VALUE(data, "$.originalDocumentNumber")' => $item['DocumentNumber']]))
                ->andWhere('YEAR(date_created) = YEAR(CURDATE())')
                ->execute()->fetchOne();

            if (!$orderId) {
                $this->log("Nothing found for DocumentNumber={$item['DocumentNumber']};CouponNumber={$item['DiscountNumber']}");
                continue;
            }

            $order = \Order::get($orderId);

            $sellerId = null;
            if ($tmpSellerId = $order->getDeliveryType()->getDelivery()->getPointId()) {
                $sellerId = (int) $tmpSellerId;
            }

            $synchronizer->setOrderCoupon($order, ['data' => ['ShopVoucherNumber' => $item['DiscountNumber']]], $sellerId);
        }
    }
}

return ImportCouponsToPOSOrdersScript::class;
