<?php

declare(strict_types=1);

namespace External\PompoBundle\Util\Export;

use External\PompoBundle\Util\Configuration;
use External\PompoBundle\Util\Ordering\OrderType;
use Query\QueryBuilder;

class OrdersExcelExport extends \KupShop\OrderingBundle\OrdersExcelExport
{
    /** @required */
    public Configuration $configuration;

    protected function prepareFields()
    {
        $fields = parent::prepareFields();

        $prependFields = [];
        if ($this->configuration->isPompo()) {
            $prependFields = [
                'drsUserId' => [
                    'name' => 'Reg. zákazník',
                    'spec' => function (QueryBuilder $qb) {
                        $qb->leftJoin('o', 'drs_users', 'du', 'du.id_user = o.id_user');

                        return 'du.id_drs as drsUserId';
                    },
                ],
                'sellerBranchId' => [
                    'name' => 'Cílová prodejna',
                    'spec' => function (QueryBuilder $qb) {
                        $qb->leftJoin('o', 'order_sellers', 'os', 'o.id = os.id_order')
                            ->leftJoin('os', 'sellers', 'sell', 'sell.id = os.id_seller');

                        return 'JSON_VALUE(sell.data, "$.branchId") as sellerBranchId';
                    },
                ],
                'expectedTod' => [
                    'name' => 'Datum závozu',
                    'spec' => function () {
                        return 'IF(JSON_VALUE(o.note_admin, "$.orderType")="'.OrderType::ORDER_TRANSPORT_RESERVATION.'", JSON_VALUE(o.note_admin, "$.delivery_data.deliveryDate"), "") as expectedTod';
                    },
                ],
                'originOfGoods' => [
                    'name' => 'Odbavuje',
                    'spec' => function () {
                        return 'IF(JSON_VALUE(o.note_admin, "$.orderType")="'.OrderType::ORDER_RESERVATION.'", "Prodejna", "Centrala") as originOfGoods';
                    },
                ],
            ];
        }

        return array_merge($prependFields, $fields);
    }
}
