<?php

declare(strict_types=1);

namespace External\HannahBundle\Resources\upgrade;

use Query\Operator;
use Query\Order;

class OutdoorUpgrade extends \UpgradeNew
{
    protected $priority = 1000;

    public function check_OutdoorconceptSettings(): bool
    {
        return !sqlQueryBuilder()
            ->select('key_name')
            ->from('settings')
            ->where(Operator::equals(['key_name' => 'outdoorconcept']))
            ->execute()->fetchOne();
    }

    /** settings: add default configuration for outdoorconcept setting */
    public function upgrade_OutdoorconceptSettings(): void
    {
        sqlQueryBuilder()
            ->insert('settings')
            ->directValues(
                [
                    'key_name' => 'outdoorconcept',
                    'value' => '{"deliveryTime":{"carrier":{"inSellerStoreIncrement":"2"},"seller":{"inMainStore":{"days":"2","afternoonIncrement":"1"},"inOtherStore":{"days":"4","afternoonIncrement":"1"}}}}',
                ]
            )->execute();

        \Settings::clearCache();

        $this->upgradeOK();
    }

    public function check_MigrateToEngineSellers(): bool
    {
        return findModule(\Modules::SELLERS) && !$this->checkColumnExists('delivery_type_delivery', 'id_seller');
    }

    /** Migrate from `sellers_old` module to `sellers` module */
    public function upgrade_MigrateToEngineSellers(): void
    {
        // Nastavim objednavkam id_seller do custom dat
        sqlQueryBuilder()
            ->update('orders', 'o')
            ->set('o.note_admin', 'JSON_MERGE_PATCH(COALESCE(o.note_admin, "{}"), CONCAT(\'{"delivery_data": {"seller_id": \', dtd.id_seller, \'}}\'))')
            ->join('o', 'delivery_type', 'dt', 'dt.id = o.id_delivery')
            ->join('dt', 'delivery_type_delivery', 'dtd', 'dtd.id = dt.id_delivery')
            ->andWhere(Order::byInPersonDelivery())
            ->andWhere('dtd.id_seller IS NOT NULL')
            ->execute();

        sqlQuery('INSERT IGNORE INTO order_sellers (id_order, id_seller)
            SELECT
                id,
                JSON_VALUE(note_admin, "$.delivery_data.seller_id")
            FROM orders
            WHERE JSON_VALUE(note_admin, "$.delivery_data.seller_id") IS NOT NULL AND JSON_VALUE(note_admin, "$.delivery_data.seller_id") != \'null\'');

        // Nastavim spravnej sklad na prodejne sellers.id_store
        // Migrovat SAP kod a Shipping point od Osobniho odberu na prodejnu
        sqlQueryBuilder()
            ->update('sellers', 'sell')
            ->set('sell.id_store', 's.id')
            ->join('sell', 'delivery_type_delivery', 'dtd', 'dtd.id_seller = sell.id')
            ->join('dtd', 'stores', 's', 's.id_delivery = dtd.id')
            ->execute();

        // Prenesu mesto z stores na sellers, protoze dava vetsi smysl, aby to nastavovali na prodejcich
        sqlQueryBuilder()
            ->update('sellers', 'sell')
            ->set('sell.data', 'JSON_SET(COALESCE(sell.data, "{}"), "$.city", JSON_VALUE(s.data, "$.city"))')
            ->join('sell', 'stores', 's', 's.id = sell.id_store')
            ->where('JSON_VALUE(s.data, "$.city") IS NOT NULL AND JSON_VALUE(s.data, "$.city") != ""')
            ->execute();

        // Dropnu id_seller na doprave (delivery_type_delivery)
        sqlQuery('ALTER TABLE delivery_type_delivery DROP COLUMN id_seller');

        $this->upgradeOK();
    }
}
