<?php

declare(strict_types=1);

/**
 * Copyright 2024-2026 Simply.IN Sp. z o.o.
 *
 * NOTICE OF LICENSE
 *
 * Licensed under the EUPL-1.2 or later.
 * You may not use this work except in compliance with the Licence.
 *
 * Copy of the Licence is available at:
 * https://joinup.ec.europa.eu/software/page/eupl
 * It is bundled with this package in the file LICENSE.txt
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Licence is distributed on an as is basis,
 * without warranties or conditions of any kind, either express or implied.
 * Check the Licence for the specific language governing permissions
 * and limitations under the License.
 *
 * @author   Simply.IN Sp. z o.o.
 * @copyright 2024-2026 Simply.IN Sp. z o.o.
 * @license   https://joinup.ec.europa.eu/software/page/eupl
 */

if (!defined('_PS_VERSION_')) {
    exit;
}

/**
 * 8.1.24 — single package, runtime environment selector (Production/Test) replaces the old
 * per-environment build with the backend URL pinned in code. An already-installed shop has a
 * stored API key and no environment setting yet; see {@see \SimplyIn\Install\EnvironmentAdopter}
 * for how the correct environment is adopted without guessing.
 *
 * Best-effort only, and its result is deliberately NOT propagated as this function's return
 * value: PrestaShop's module-upgrade runner (Module::runUpgradeModule()) disables the module when
 * an upgrade function returns false, and the modern admin flow (Core\Module\ModuleManager::
 * upgradeMigration()) bumps the stored module version unconditionally after running it regardless
 * of that return value — so "return false to make it retry" neither retries (there is no later
 * run through this path) nor does anything less drastic than disabling checkout over a network
 * blip during one probe. The actual retry lives in Simplyin::attemptEnvironmentAdoption(), a
 * throttled admin-only hook that keeps trying independently of this one-off upgrade step, exactly
 * like the existing RSA registration backstop already does for its own retry.
 *
 * @param \Simplyin $module
 */
function upgrade_module_8_1_24($module)
{
    (new \SimplyIn\Install\EnvironmentAdopter(new \SimplyIn\BackOffice\ConfigurationRepository()))->adopt(
        (string) \Configuration::get('SIMPLYIN_SECRET_KEY'),
        (string) \Context::getContext()->shop->getBaseURL(true),
        (string) $module->version,
        (string) \Configuration::get('PS_SHOP_NAME'),
    );

    return true;
}
