<?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',
    ];

    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 {
        $this->session->set('simplyin_payment_type', $paymentType);
        $this->session->set('simplyin_payment_id', $paymentId);
        $this->session->set('simplyin_payment_provider', $paymentProvider);
        $this->session->set('simplyin_payment_image', $providerImage);
        $this->session->set('simplyin_payment_logo', $logo);

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

            return;
        }

        if ($paymentType === 'card') {
            $this->session->set('simplyin_card_name', $cardName);
            $this->session->set('simplyin_card_last_digits', $cardLastDigits);
            $this->session->set('simplyin_card_provider', $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
    {
        $this->session->set('simplyin_payment_blik', $code);
    }

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