<?php

/**
 * Copyright 2024-2025 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-2025 Simply.IN Sp. z o.o.
 * @license   https://joinup.ec.europa.eu/software/page/eupl
 */

declare(strict_types=1);

namespace SimplyIn\BackOffice;

use Configuration;

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

final class ConfigurationRepository
{
    public function getString(ConfigurationKey $key, string $default = ''): string
    {
        $value = Configuration::get($key->value);
        if ($value === false) {
            return $default;
        }

        return (string) $value;
    }

    public function getInt(ConfigurationKey $key, int $default = 0): int
    {
        $value = Configuration::get($key->value);
        if ($value === false || $value === '') {
            return $default;
        }

        return (int) $value;
    }

    public function getBool(ConfigurationKey $key, bool $default = false): bool
    {
        $value = Configuration::get($key->value);
        if ($value === false || $value === '') {
            return $default;
        }

        return (bool) (int) $value;
    }

    public function set(ConfigurationKey $key, mixed $value): bool
    {
        return Configuration::updateValue($key->value, $value);
    }

    public function isConnected(): bool
    {
        return $this->getBool(ConfigurationKey::CONNECTED);
    }

    /** The shop's selected environment, resolved through the closed allowlist ({@see Environment::fromStoredValue()}) so an unset or corrupted value always falls back to Production. */
    public function getEnvironment(): Environment
    {
        return Environment::fromStoredValue($this->getString(ConfigurationKey::ENVIRONMENT));
    }

    public function setEnvironment(Environment $environment): void
    {
        $this->set(ConfigurationKey::ENVIRONMENT, $environment->value);
    }

    /** The API key stored for the given environment's own slot — never the other environment's. */
    public function getApiKey(Environment $environment): string
    {
        return $this->getString($environment->apiKeyConfigurationKey());
    }

    public function setApiKey(Environment $environment, string $apiKey): void
    {
        $this->set($environment->apiKeyConfigurationKey(), $apiKey);
    }

    /**
     * True only for a shop upgrading from the old per-environment build whose one-off environment
     * adoption ({@see \SimplyIn\Install\EnvironmentAdopter}) has not resolved yet — a stored legacy
     * key with no environment setting. A fresh install always has an explicit environment (set at
     * install time), so this can never be true for one; {@see getEnvironment()}'s fallback to
     * Production is the correct read for a fresh install, but NOT for this state — here it would
     * be a guess, not a safe default, which is why bootstrap treats this shop as unauthenticated
     * (no API key) rather than silently picking an environment to send real traffic to.
     */
    public function isEnvironmentAdoptionPending(): bool
    {
        return $this->getString(ConfigurationKey::ENVIRONMENT) === ''
            && $this->getString(ConfigurationKey::SECRET_KEY) !== '';
    }
}
