<?php

/**
 * 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
 */

declare(strict_types=1);

namespace SimplyIn\BackOffice;

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

/**
 * The two Simply.IN environments this plugin is allowed to talk to — a closed allowlist, not a
 * free-text setting. Everything the plugin needs to know about "which environment" (the backend
 * URL, the matching merchant panel, where the API key for it is stored) is derived from here, so
 * no call site hardcodes a URL or duplicates the mapping.
 *
 * Deliberately only two cases: a live shop left on the wrong one sends buyer data to a
 * non-production environment, so a third (e.g. a free-text URL, or the internal dev environment)
 * must never be reachable through this enum.
 */
enum Environment: string
{
    case PRODUCTION = 'production';
    case TEST = 'test';

    /** A fresh install — or any stored value outside the allowlist — must always resolve here. */
    public static function default(): self
    {
        return self::PRODUCTION;
    }

    /**
     * Resolves a stored (or submitted) value through the allowlist. Anything not exactly one of
     * the two known cases — empty, corrupted, or a value from a removed option — falls back to
     * the safe default instead of being trusted as-is.
     */
    public static function fromStoredValue(string $value): self
    {
        return self::tryFrom($value) ?? self::default();
    }

    public function label(): string
    {
        return match ($this) {
            self::PRODUCTION => 'Production',
            self::TEST => 'Test',
        };
    }

    public function backendUrl(): string
    {
        return match ($this) {
            self::PRODUCTION => 'https://prod.backend.simply.in/api/',
            self::TEST => 'https://preprod.backend.simplyin.app/api/',
        };
    }

    public function merchantPanelUrl(): string
    {
        return match ($this) {
            self::PRODUCTION => 'https://merchant.simply.in',
            self::TEST => 'https://merchant.simplyin.app',
        };
    }

    /** Where this environment's API key is stored — a dedicated slot per environment (never one shared field), so switching environments cannot destroy the other one's key. */
    public function apiKeyConfigurationKey(): ConfigurationKey
    {
        return match ($this) {
            self::PRODUCTION => ConfigurationKey::API_KEY_PRODUCTION,
            self::TEST => ConfigurationKey::API_KEY_TEST,
        };
    }

    /**
     * Namespaces this environment's request-signing credential storage, for the same reason the
     * API key gets its own slot: the credential is registered against one environment's backend,
     * so one shared slot would mean switching environments silently invalidates the other one's
     * registration (and the backend, which still holds it, then refuses to re-register it).
     *
     * Production deliberately keeps the historical unsuffixed names, so an already-registered
     * shop needs no migration of its key material.
     */
    public function rsaSlotSuffix(): string
    {
        return match ($this) {
            self::PRODUCTION => '',
            self::TEST => '_TEST',
        };
    }
}
