<?php

namespace KupShop\OrderingBundle\Resources\upgrade;

use KupShop\KupShopBundle\Config;

class ThePay20Upgrade extends \UpgradeNew
{
    public function check_thepay20()
    {
        return $this->checkDataMigration(15);
    }

    /** Migrate payment statuses config to settings */
    public function upgrade_thepay20()
    {
        if (findModule(\Modules::PAYMENTS)) {
            $cfg = Config::get();
            $dbcfg = \Settings::getDefault();
            $payments = $dbcfg['payment_config'] ?? [];
            if (!empty($cfg['Modules']['payments'])) {
                foreach ($cfg['Modules']['payments'] as $payment) {
                    var_dump($payment);
                    if (isset($payment['order_status_new'])) {
                        $payments['order_status_new'] = $payment['order_status_new'];
                    } else {
                        $payments['order_status_new'] = '0';
                    }

                    if (array_key_exists('order_status_finished', $payment)) {
                        $payments['order_status_finished'] = $payment['order_status_finished'] ?? 'keep';
                    } else {
                        $payments['order_status_finished'] = '1';
                    }

                    $dbcfg->saveValue('payment_config', $payments);
                    $dbcfg->clearCache();
                    break;
                }
            }
        }

        $this->commitDataMigration(15);
        $this->upgradeOK();
    }
}
