<?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\Form\DataProvider;

use SimplyIn\BackOffice\ConfigurationKey;
use SimplyIn\BackOffice\ConfigurationRepository;

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

final class SettingsDataProvider
{
    private const PERSISTED_FIELDS = [
        'secret_key' => ConfigurationKey::SECRET_KEY,
        'save_checkbox' => ConfigurationKey::SAVE_CHECKBOX,
        'payments_enabled' => ConfigurationKey::PAYMENTS_ENABLED,
        'dpd_carrier_id' => ConfigurationKey::DPD_CARRIER_ID,
        'dhl_carrier_id' => ConfigurationKey::DHL_CARRIER_ID,
        'fedex_carrier_id' => ConfigurationKey::FEDEX_CARRIER_ID,
        'inpost_carrier_id' => ConfigurationKey::INPOST_CARRIER_ID,
        'orlen_carrier_id' => ConfigurationKey::ORLEN_CARRIER_ID,
        'ups_carrier_id' => ConfigurationKey::UPS_CARRIER_ID,
        'default_carrier_id' => ConfigurationKey::DEFAULT_CARRIER_ID,
        'payment_status_pending' => ConfigurationKey::PAYMENT_STATUS_PENDING,
        'payment_status_canceled' => ConfigurationKey::PAYMENT_STATUS_CANCELED,
        'bar_enabled' => ConfigurationKey::BAR_ENABLED,
        'bar_color' => ConfigurationKey::BAR_COLOR,
        'badge_color' => ConfigurationKey::BADGE_COLOR,
        'accent_color' => ConfigurationKey::ACCENT_COLOR,
        'font_color' => ConfigurationKey::FONT_COLOR,
    ];

    private const BOOLEAN_FIELDS = ['save_checkbox', 'payments_enabled', 'bar_enabled', 'bar_color', 'badge_color'];

    private const STRING_DEFAULTS = [
        'accent_color' => '#2d40f6',
        'font_color' => '#000000',
    ];
    private const INTEGER_FIELDS = [
        'dpd_carrier_id',
        'dhl_carrier_id',
        'fedex_carrier_id',
        'inpost_carrier_id',
        'orlen_carrier_id',
        'ups_carrier_id',
        'default_carrier_id',
        'payment_status_pending',
        'payment_status_canceled',
    ];

    public function __construct(
        private readonly ConfigurationRepository $config,
    ) {
    }

    /**
     * @return array<string, mixed>
     */
    public function getData(): array
    {
        $data = [];
        foreach (self::PERSISTED_FIELDS as $field => $key) {
            if (in_array($field, self::BOOLEAN_FIELDS, true)) {
                $data[$field] = $this->config->getBool($key);
            } elseif (in_array($field, self::INTEGER_FIELDS, true)) {
                $data[$field] = $this->config->getInt($key);
            } else {
                $value = $this->config->getString($key);
                $data[$field] = ($value === '' && isset(self::STRING_DEFAULTS[$field]))
                    ? self::STRING_DEFAULTS[$field]
                    : $value;
            }
        }

        return $data;
    }

    /**
     * @param array<string, mixed> $data
     */
    public function saveData(array $data): void
    {
        foreach (self::PERSISTED_FIELDS as $field => $key) {
            if (!array_key_exists($field, $data)) {
                continue;
            }
            $value = $data[$field];
            if (in_array($field, self::BOOLEAN_FIELDS, true)) {
                $this->config->set($key, $value ? 1 : 0);
            } elseif (in_array($field, self::INTEGER_FIELDS, true)) {
                $this->config->set($key, (int) $value);
            } else {
                $this->config->set($key, (string) $value);
            }
        }
    }
}
