<?php
/**
 * NOTICE OF LICENSE
 *
 * @author Mondial Relay <offrestart@mondialrelay.fr>
 * @copyright Copyright (c) Mondial Relay
 * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
 */
if (!defined('_PS_VERSION_')) {
    exit;
}

/**
 * Upgrade to 4.2.0
 *
 * Regroups previous 4.1.x updates.
 *
 * @param Mondialrelay $module
 * @return bool
 */
function upgrade_module_4_2_0($module)
{
    require_once _PS_MODULE_DIR_ . '/mondialrelay/classes/MondialrelayReturnLabel.php';

    $installer = new MondialrelayClasslib\Install\ModuleInstaller($module);

    $module->setConfigurationDefault(Mondialrelay::RETURN_ENABLED, 0);
    $module->setConfigurationDefault(Mondialrelay::RETURN_LABEL_DELIVERY_MODE, 'customer_account');
    $module->setConfigurationDefault(Mondialrelay::RETURN_DEFAULT_RELAY, '');
    $module->setConfigurationDefault(Mondialrelay::RETURN_DEFAULT_RELAY_DATA, '');

    if (!$installer->installAdminControllers()) {
        return false;
    }

    if (!$module->ensureAdminTabsOrder(true)) {
        return false;
    }

    if (!$installer->installObjectModel('MondialrelayReturnLabel')) {
        return false;
    }

    if (!Db::getInstance()->execute(
        'ALTER TABLE `' . _DB_PREFIX_ . 'mondialrelay_selected_relay`
            MODIFY `label_url` VARCHAR(4000) NULL'
    )) {
        return false;
    }

    return true;
}
