<?php

use Openguru\OpenCartFramework\Migrations\Migration;
use Openguru\OpenCartFramework\OpenCart\Decorators\OcRegistryDecorator;
use Openguru\OpenCartFramework\Support\Arr;

return new class extends Migration {
    public function up(): void
    {
        $opencart = $this->app->get(OcRegistryDecorator::class);
        $opencart->load->model('setting/setting');

        $legacySettings = $opencart->model_setting_setting->getSetting('module_tgshop');
        if (! $legacySettings) {
            return;
        }

        $newSettings = $opencart->model_setting_setting->getSetting('module_telecart');

        static $mapLegacyToNewSettings = [
            'module_tgshop_app_icon' => 'app.app_icon',
            'module_tgshop_theme_light' => 'app.theme_light',
            'module_tgshop_bot_token' => 'telegram.bot_token',
            'module_tgshop_status' => 'app.app_enabled',
            'module_tgshop_app_name' => 'app.app_name',
            'module_tgshop_theme_dark' => 'app.theme_dark',
            'module_tgshop_debug' => 'app.app_debug',
            'module_tgshop_chat_id' => 'telegram.chat_id',
            'module_tgshop_text_order_created_success' => 'texts.text_order_created_success',
            'module_tgshop_enable_store' => 'store.enable_store',
            'module_tgshop_feature_vouchers' => 'store.feature_vouchers',
            'module_tgshop_order_default_status_id' => 'orders.order_default_status_id',
            'module_tgshop_feature_coupons' => 'store.feature_coupons',
            'module_tgshop_text_no_more_products' => 'texts.text_no_more_products',
            'module_tgshop_text_empty_cart' => 'texts.text_empty_cart',
        ];

        if (! $newSettings) {
            $data = [];

            foreach ($mapLegacyToNewSettings as $key => $value) {
                if (array_key_exists($key, $legacySettings)) {
                    if ($key === 'module_tgshop_status') {
                        $newValue = filter_var($legacySettings[$key], FILTER_VALIDATE_BOOLEAN);
                    } elseif ($key === 'module_tgshop_debug') {
                        $newValue = filter_var($legacySettings[$key], FILTER_VALIDATE_BOOLEAN);
                    } elseif ($key === 'module_tgshop_chat_id') {
                        $newValue = (int) $legacySettings[$key];
                    } elseif ($key === 'module_tgshop_enable_store') {
                        $newValue = filter_var($legacySettings[$key], FILTER_VALIDATE_BOOLEAN);
                    } elseif ($key === 'module_tgshop_order_default_status_id') {
                        $newValue = (int) $legacySettings[$key];
                    } elseif ($key === 'module_tgshop_feature_vouchers') {
                        $newValue = filter_var($legacySettings[$key], FILTER_VALIDATE_BOOLEAN);
                    } elseif ($key === 'module_tgshop_feature_coupons') {
                        $newValue = filter_var($legacySettings[$key], FILTER_VALIDATE_BOOLEAN);
                    } else {
                        $newValue = $legacySettings[$key];
                    }

                    Arr::set($data, $value, $newValue);
                }
            }

            $opencart->model_setting_setting->editSetting('module_telecart', [
                'module_telecart_settings' => $data,
            ]);

            $this->logger->info('Выполнено обновление настроек с 1й версии модуля.');
        }

        $opencart->model_setting_setting->deleteSetting('module_tgshop');
    }
};
