<?php
/**
 * 2026 Paysera
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * @author Paysera <plugins@paysera.com>
 * @copyright 2026 Paysera
 * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
 * International Registered Trademark & Property of Paysera
 */

declare(strict_types=1);

namespace Paysera\PrestashopPayments\Service\Payment;

use Paysera\PrestashopPayments\Checker\ModuleConflictChecker;
use Paysera\PrestashopPayments\Form\PaymentSettingsDataConfiguration;

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

class PaymentSettingsService
{
    public function __construct(
        private PaymentSettingsDataConfiguration $paymentSettingsConfig,
        private ModuleConflictChecker $moduleConflictChecker
    ) {
    }

    public function updatePaymentSettings(array $requestData): array
    {
        $configuration = $this->mapRequestToConfiguration($requestData);

        if ($this->isEnablingPaymentsWithConflict($configuration)) {
            return [
                'success' => false,
                'errors' => ['Only one version of the Paysera Checkout plugin can be active at a time. Please deactivate the currently enabled version before activating another.'],
            ];
        }

        $errors = $this->paymentSettingsConfig->updateConfiguration($configuration);

        if (count($errors) > 0) {
            return [
                'success' => false,
                'errors' => $errors,
            ];
        }

        return [
            'success' => true,
            'data' => $this->paymentSettingsConfig->getConfiguration(),
        ];
    }

    private function mapRequestToConfiguration(array $requestData): array
    {
        $configuration = [];

        if (isset($requestData['enable_payments'])) {
            $configuration['enable_payments'] = (bool) $requestData['enable_payments'];
        }

        if (isset($requestData['payment_window_mode'])) {
            $configuration['payment_window_mode'] = (string) $requestData['payment_window_mode'];
        }

        if (isset($requestData['payment_title'])) {
            $configuration['payment_title'] = (string) $requestData['payment_title'];
        }

        if (isset($requestData['status_awaiting_payment'])) {
            $configuration['status_awaiting_payment'] = (string) $requestData['status_awaiting_payment'];
        }

        if (isset($requestData['status_awaiting_confirmation'])) {
            $configuration['status_awaiting_confirmation'] = (string) $requestData['status_awaiting_confirmation'];
        }

        if (isset($requestData['status_confirmed_payment'])) {
            $configuration['status_confirmed_payment'] = (string) $requestData['status_confirmed_payment'];
        }

        return $configuration;
    }

    private function isEnablingPaymentsWithConflict(array $configuration): bool
    {
        return isset($configuration['enable_payments'])
            && $configuration['enable_payments'] === true
            && $this->moduleConflictChecker->hasConflict();
    }
}
