<?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\Carrier;

use Carrier;
use Cart;
use Configuration;
use Context;
use Currency;

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

final class CarrierConfigRegistry
{
    /**
     * @var array<string, string> configuration key -> provider slug
     */
    private const CARRIER_MAP = [
        'SIMPLYIN_DPD_CARRIER_ID' => 'dpd',
        'SIMPLYIN_UPS_CARRIER_ID' => 'ups',
        'SIMPLYIN_DHL_CARRIER_ID' => 'dhl',
        'SIMPLYIN_FEDEX_CARRIER_ID' => 'fedex',
        'SIMPLYIN_INPOST_CARRIER_ID' => 'inpost',
        'SIMPLYIN_ORLEN_CARRIER_ID' => 'orlen',
        'SIMPLYIN_DEFAULT_CARRIER_ID' => 'default',
    ];

    /**
     * @return array<int, string>
     */
    public function getCarrierKeys(): array
    {
        return array_keys(self::CARRIER_MAP);
    }

    /**
     * @return array<int, string> slugs ('dpd','ups','dhl','fedex','inpost','orlen') for each configured provider
     */
    public function getAvailableCarriers(): array
    {
        $available = [];
        foreach (self::CARRIER_MAP as $key => $slug) {
            if ($slug === 'default') {
                continue;
            }
            if (Configuration::get($key)) {
                $available[] = $slug;
            }
        }

        return $available;
    }

    /**
     * @return array<int, array<string, mixed>>
     */
    public function getAllAvailableShippingMethods(int $languageId): array
    {
        return Carrier::getCarriers($languageId, true, false, false, null, Carrier::ALL_CARRIERS);
    }

    /**
     * @return array<string, string> slug -> formatted price
     */
    public function getCartCarrierCost(Context $context): array
    {
        $cart = $context->cart;
        if ($cart === null) {
            return [];
        }

        $carriers = [];
        foreach (self::CARRIER_MAP as $key => $slug) {
            $idCarrier = Configuration::get($key);
            if ($idCarrier) {
                $carriers[$slug] = (int) $idCarrier;
            }
        }

        $currencyIso = Currency::getIsoCodeById((int) $cart->id_currency);
        $costs = [];
        foreach ($carriers as $slug => $idCarrier) {
            $costs[$slug] = $context->getCurrentLocale()->formatPrice(
                $cart->getOrderTotal(true, Cart::ONLY_SHIPPING, null, $idCarrier),
                $currencyIso,
            );
        }

        return $costs;
    }

    public function handleCarrierUpdate(int $carrierId): void
    {
        foreach (array_keys(self::CARRIER_MAP) as $key) {
            if ((int) Configuration::get($key) === $carrierId) {
                Configuration::updateValue($key, 0);

                return;
            }
        }
    }
}
