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

use SimplyIn\Session\SimplyInSessionInterface;

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

final class PaymentSelectionStore
{
    /** @var list<string> */
    private const CARD_KEYS = [
        'simplyin_card_name',
        'simplyin_card_last_digits',
        'simplyin_card_provider',
        'simplyin_card_ott',
        'simplyin_card_save',
    ];

    private const MAX_FIELD_LENGTH = 2048;

    public function __construct(private readonly SimplyInSessionInterface $session)
    {
    }

    public function storeSelection(
        string $paymentType,
        string $paymentId,
        string $paymentProvider,
        string $providerImage,
        string $logo,
        string $cardName = '',
        string $cardLastDigits = '',
        string $cardProvider = '',
        string $cardOtt = '',
        bool $cardSave = false,
    ): void {
        if (preg_match('/^\d{4}$/', $cardLastDigits) !== 1) {
            $cardLastDigits = '';
        }

        $this->session->set('simplyin_payment_type', $this->cap($paymentType));
        $this->session->set('simplyin_payment_id', $this->cap($paymentId));
        $this->session->set('simplyin_payment_provider', $this->cap($paymentProvider));
        $this->session->set('simplyin_payment_image', $this->cap($providerImage));
        $this->session->set('simplyin_payment_logo', $this->cap($logo));

        if ($paymentType === 'add-card') {
            $this->session->set('simplyin_card_name', $this->cap($cardName));
            $this->session->set('simplyin_card_last_digits', $cardLastDigits);
            $this->session->set('simplyin_card_provider', $this->cap($cardProvider));
            $this->session->set('simplyin_card_ott', $this->cap($cardOtt));
            $this->session->set('simplyin_card_save', $cardSave ? 1 : 0);

            return;
        }

        if ($paymentType === 'card') {
            $this->session->set('simplyin_card_name', $this->cap($cardName));
            $this->session->set('simplyin_card_last_digits', $cardLastDigits);
            $this->session->set('simplyin_card_provider', $this->cap($cardProvider));
            $this->session->unset('simplyin_card_ott');
            $this->session->unset('simplyin_card_save');

            return;
        }

        $this->session->unset(self::CARD_KEYS);
    }

    public function storeBlikCode(string $code): void
    {
        if (preg_match('/^\d{6}$/', $code) !== 1) {
            $this->clearBlikCode();

            return;
        }

        $this->session->set('simplyin_payment_blik', $code);
    }

    private function cap(string $value): string
    {
        return substr($value, 0, self::MAX_FIELD_LENGTH);
    }

    public function clearBlikCode(): void
    {
        $this->session->unset('simplyin_payment_blik');
    }
}
